2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-11 04:31:10 -04:00
|
|
|
package role
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-04-11 04:31:10 -04:00
|
|
|
)
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2022-04-11 04:31:10 -04:00
|
|
|
func TestMarshal(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
role Role
|
2022-07-08 04:59:59 -04:00
|
|
|
wantJSON string
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-04-11 04:31:10 -04:00
|
|
|
}{
|
2022-06-29 09:26:29 -04:00
|
|
|
"controlePlane role": {
|
|
|
|
role: ControlPlane,
|
2022-07-08 04:59:59 -04:00
|
|
|
wantJSON: `"ControlPlane"`,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
"node role": {
|
2022-06-29 09:26:29 -04:00
|
|
|
role: Worker,
|
2022-07-08 04:59:59 -04:00
|
|
|
wantJSON: `"Worker"`,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
"unknown role": {
|
2022-04-26 10:54:05 -04:00
|
|
|
role: Unknown,
|
2022-07-08 04:59:59 -04:00
|
|
|
wantJSON: `"Unknown"`,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
jsonRole, err := tc.role.MarshalJSON()
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 04:31:10 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
2022-07-08 04:59:59 -04:00
|
|
|
assert.Equal(tc.wantJSON, string(jsonRole))
|
2022-04-11 04:31:10 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshal(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
json string
|
|
|
|
wantRole Role
|
|
|
|
wantErr bool
|
2022-04-11 04:31:10 -04:00
|
|
|
}{
|
2022-06-29 09:26:29 -04:00
|
|
|
"ControlPlane can be unmarshaled": {
|
|
|
|
json: `"ControlPlane"`,
|
|
|
|
wantRole: ControlPlane,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
2022-10-06 05:57:35 -04:00
|
|
|
"dashed ControlPlane can be unmarshaled": {
|
|
|
|
json: `"Control-Plane"`,
|
|
|
|
wantRole: ControlPlane,
|
|
|
|
},
|
2022-06-29 09:26:29 -04:00
|
|
|
"lowercase controlPlane can be unmarshaled": {
|
|
|
|
json: `"controlPlane"`,
|
|
|
|
wantRole: ControlPlane,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
2022-06-29 09:26:29 -04:00
|
|
|
"Worker can be unmarshaled": {
|
|
|
|
json: `"Worker"`,
|
|
|
|
wantRole: Worker,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
2022-06-29 09:26:29 -04:00
|
|
|
"lowercase worker can be unmarshaled": {
|
|
|
|
json: `"worker"`,
|
|
|
|
wantRole: Worker,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
"other strings unmarshal to the unknown role": {
|
2022-04-26 10:54:05 -04:00
|
|
|
json: `"anything"`,
|
|
|
|
wantRole: Unknown,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
"invalid json fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
json: `"unterminated string literal`,
|
|
|
|
wantErr: true,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
var role Role
|
|
|
|
err := role.UnmarshalJSON([]byte(tc.json))
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 04:31:10 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantRole, role)
|
2022-04-11 04:31:10 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|