File: stringsource_test.go

package info (click to toggle)
golang-github-openshift-api 4.0%2Bgit20190508.81d064c-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 14,040 kB
  • sloc: sh: 171; makefile: 32
file content (149 lines) | stat: -rw-r--r-- 4,817 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package v1

import (
	"fmt"
	"reflect"
	"strings"
	"testing"

	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/serializer"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"

	configv1 "github.com/openshift/api/config/v1"
)

var testScheme = runtime.NewScheme()

func init() {
	utilruntime.Must(Install(testScheme))
}

func TestStringSourceUnmarshaling(t *testing.T) {
	codec := serializer.NewCodecFactory(testScheme).LegacyCodec(GroupVersion)

	testcases := map[string]struct {
		JSON           string
		ExpectedObject configv1.StringSource
		ExpectedError  string
	}{
		"bool": {
			JSON:           `true`,
			ExpectedObject: configv1.StringSource{},
			ExpectedError:  "cannot unmarshal",
		},
		"number": {
			JSON:           `1`,
			ExpectedObject: configv1.StringSource{},
			ExpectedError:  "cannot unmarshal",
		},

		"empty string": {
			JSON:           `""`,
			ExpectedObject: configv1.StringSource{},
			ExpectedError:  "",
		},
		"string": {
			JSON:           `"foo"`,
			ExpectedObject: configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Value: "foo"}},
			ExpectedError:  "",
		},

		"empty struct": {
			JSON:           `{}`,
			ExpectedObject: configv1.StringSource{},
			ExpectedError:  "",
		},
		"struct value": {
			JSON:           `{"value":"foo"}`,
			ExpectedObject: configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Value: "foo"}},
			ExpectedError:  "",
		},
		"struct env": {
			JSON:           `{"env":"foo"}`,
			ExpectedObject: configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Env: "foo"}},
			ExpectedError:  "",
		},
		"struct file": {
			JSON:           `{"file":"foo"}`,
			ExpectedObject: configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{File: "foo"}},
			ExpectedError:  "",
		},
		"struct file+keyFile": {
			JSON:           `{"file":"foo","keyFile":"bar"}`,
			ExpectedObject: configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{File: "foo", KeyFile: "bar"}},
			ExpectedError:  "",
		},
	}

	for k, tc := range testcases {
		t.Run(k, func(t *testing.T) {
			// Wrap in a dummy object we can deserialize
			input := fmt.Sprintf(`{"kind":"GitHubIdentityProvider","apiVersion":"osin.config.openshift.io/v1","clientSecret":%s}`, tc.JSON)
			githubProvider := &GitHubIdentityProvider{}
			err := runtime.DecodeInto(codec, []byte(input), githubProvider)
			if len(tc.ExpectedError) > 0 && (err == nil || !strings.Contains(err.Error(), tc.ExpectedError)) {
				t.Errorf("%s: expected error containing %q, got %q", k, tc.ExpectedError, err.Error())
			}
			if len(tc.ExpectedError) == 0 && err != nil {
				t.Errorf("%s: got unexpected error: %v", k, err)
			}
			if err != nil {
				return
			}
			if !reflect.DeepEqual(tc.ExpectedObject, githubProvider.ClientSecret) {
				t.Errorf("%s: expected\n%#v\ngot\n%#v", k, tc.ExpectedObject, githubProvider.ClientSecret)
			}
		})
	}
}

func TestStringSourceMarshaling(t *testing.T) {
	codec := serializer.NewCodecFactory(testScheme).LegacyCodec(GroupVersion)

	testcases := map[string]struct {
		Object       configv1.StringSource
		ExpectedJSON string
	}{
		"empty string": {
			Object:       configv1.StringSource{},
			ExpectedJSON: `""`,
		},
		"string": {
			Object:       configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Value: "foo"}},
			ExpectedJSON: `"foo"`,
		},
		"struct value+keyFile": {
			Object:       configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Value: "foo", KeyFile: "bar"}},
			ExpectedJSON: `{"value":"foo","env":"","file":"","keyFile":"bar"}`,
		},
		"struct env": {
			Object:       configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Env: "foo"}},
			ExpectedJSON: `{"value":"","env":"foo","file":"","keyFile":""}`,
		},
		"struct file": {
			Object:       configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{File: "foo"}},
			ExpectedJSON: `{"value":"","env":"","file":"foo","keyFile":""}`,
		},
		"struct file+keyFile": {
			Object:       configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{File: "foo", KeyFile: "bar"}},
			ExpectedJSON: `{"value":"","env":"","file":"foo","keyFile":"bar"}`,
		},
	}

	for k, tc := range testcases {
		provider := &GitHubIdentityProvider{ClientSecret: tc.Object}

		json, err := runtime.Encode(codec, provider)
		if err != nil {
			t.Errorf("%s: unexpected error: %v", k, err)
		}

		// Wrap in a dummy JSON from the surrounding object
		input := fmt.Sprintf(`{"kind":"GitHubIdentityProvider","apiVersion":"osin.config.openshift.io/v1","clientID":"","clientSecret":%s,"organizations":null,"teams":null,"hostname":"","ca":""}`, tc.ExpectedJSON)
		if strings.TrimSpace(string(json)) != input {
			t.Log(len(input), len(json))
			t.Errorf("%s: expected\n%s\ngot\n%s", k, input, string(json))
		}
	}
}