2022-10-11 06:24:33 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cloudprovider
|
|
|
|
|
|
|
|
import (
|
2022-11-28 04:27:33 -05:00
|
|
|
"encoding/json"
|
2022-10-11 06:24:33 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-11-28 04:27:33 -05:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-10-11 06:24:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarshalJSON(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
input Provider
|
|
|
|
want []byte
|
|
|
|
}{
|
|
|
|
"unknown": {
|
|
|
|
input: Unknown,
|
|
|
|
want: []byte("\"Unknown\""),
|
|
|
|
},
|
|
|
|
"aws": {
|
|
|
|
input: AWS,
|
|
|
|
want: []byte("\"AWS\""),
|
|
|
|
},
|
|
|
|
"azure": {
|
|
|
|
input: Azure,
|
|
|
|
want: []byte("\"Azure\""),
|
|
|
|
},
|
|
|
|
"gcp": {
|
|
|
|
input: GCP,
|
|
|
|
want: []byte("\"GCP\""),
|
|
|
|
},
|
2023-02-27 12:19:52 -05:00
|
|
|
"openstack": {
|
|
|
|
input: OpenStack,
|
|
|
|
want: []byte("\"OpenStack\""),
|
|
|
|
},
|
2022-10-11 06:24:33 -04:00
|
|
|
"qemu": {
|
|
|
|
input: QEMU,
|
|
|
|
want: []byte("\"QEMU\""),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-11-28 04:27:33 -05:00
|
|
|
b, err := json.Marshal(tc.input)
|
2022-10-11 06:24:33 -04:00
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.want, b)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalJSON(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
input []byte
|
|
|
|
want Provider
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"empty": {
|
|
|
|
input: []byte{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"unknown": {
|
|
|
|
input: []byte("\"unknown\""),
|
|
|
|
want: Unknown,
|
|
|
|
},
|
|
|
|
"aws": {
|
|
|
|
input: []byte("\"aws\""),
|
|
|
|
want: AWS,
|
|
|
|
},
|
|
|
|
"azure": {
|
|
|
|
input: []byte("\"azure\""),
|
|
|
|
want: Azure,
|
|
|
|
},
|
|
|
|
"gcp": {
|
|
|
|
input: []byte("\"gcp\""),
|
|
|
|
want: GCP,
|
|
|
|
},
|
2023-02-27 12:19:52 -05:00
|
|
|
"openstack": {
|
|
|
|
input: []byte("\"openstack\""),
|
|
|
|
want: OpenStack,
|
|
|
|
},
|
2022-10-11 06:24:33 -04:00
|
|
|
"qemu": {
|
|
|
|
input: []byte("\"qemu\""),
|
|
|
|
want: QEMU,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var p Provider
|
2022-11-28 04:27:33 -05:00
|
|
|
err := json.Unmarshal(tc.input, &p)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.want, p)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalYAML(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
input Provider
|
|
|
|
want []byte
|
|
|
|
}{
|
|
|
|
"unknown": {
|
|
|
|
input: Unknown,
|
|
|
|
want: []byte("Unknown\n"),
|
|
|
|
},
|
|
|
|
"aws": {
|
|
|
|
input: AWS,
|
|
|
|
want: []byte("AWS\n"),
|
|
|
|
},
|
|
|
|
"azure": {
|
|
|
|
input: Azure,
|
|
|
|
want: []byte("Azure\n"),
|
|
|
|
},
|
|
|
|
"gcp": {
|
|
|
|
input: GCP,
|
|
|
|
want: []byte("GCP\n"),
|
|
|
|
},
|
2023-02-27 12:19:52 -05:00
|
|
|
"openstack": {
|
|
|
|
input: OpenStack,
|
|
|
|
want: []byte("OpenStack\n"),
|
|
|
|
},
|
2022-11-28 04:27:33 -05:00
|
|
|
"qemu": {
|
|
|
|
input: QEMU,
|
|
|
|
want: []byte("QEMU\n"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
b, err := yaml.Marshal(tc.input)
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.want, b)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalYAML(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
input []byte
|
|
|
|
want Provider
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"empty": {
|
|
|
|
input: []byte("foo: bar\n"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"unknown": {
|
|
|
|
input: []byte("unknown\n"),
|
|
|
|
want: Unknown,
|
|
|
|
},
|
|
|
|
"aws": {
|
|
|
|
input: []byte("aws\n"),
|
|
|
|
want: AWS,
|
|
|
|
},
|
|
|
|
"azure": {
|
|
|
|
input: []byte("azure\n"),
|
|
|
|
want: Azure,
|
|
|
|
},
|
|
|
|
"gcp": {
|
|
|
|
input: []byte("gcp\n"),
|
|
|
|
want: GCP,
|
|
|
|
},
|
2023-02-27 12:19:52 -05:00
|
|
|
"openstack": {
|
|
|
|
input: []byte("openstack\n"),
|
|
|
|
want: OpenStack,
|
|
|
|
},
|
2022-11-28 04:27:33 -05:00
|
|
|
"qemu": {
|
|
|
|
input: []byte("qemu\n"),
|
|
|
|
want: QEMU,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var p Provider
|
|
|
|
err := yaml.Unmarshal(tc.input, &p)
|
2022-10-11 06:24:33 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.want, p)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromString(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
input string
|
|
|
|
want Provider
|
|
|
|
}{
|
|
|
|
"empty": {
|
|
|
|
input: "",
|
|
|
|
want: Unknown,
|
|
|
|
},
|
|
|
|
"unknown": {
|
|
|
|
input: "unknown",
|
|
|
|
want: Unknown,
|
|
|
|
},
|
|
|
|
"aws": {
|
|
|
|
input: "aws",
|
|
|
|
want: AWS,
|
|
|
|
},
|
|
|
|
"azure": {
|
|
|
|
input: "azure",
|
|
|
|
want: Azure,
|
|
|
|
},
|
|
|
|
"gcp": {
|
|
|
|
input: "gcp",
|
|
|
|
want: GCP,
|
|
|
|
},
|
2023-02-27 12:19:52 -05:00
|
|
|
"openstack": {
|
|
|
|
input: "openstack",
|
|
|
|
want: OpenStack,
|
|
|
|
},
|
2023-05-30 03:02:50 -04:00
|
|
|
"stackit": {
|
|
|
|
input: "stackit",
|
|
|
|
want: OpenStack,
|
|
|
|
},
|
2022-10-11 06:24:33 -04:00
|
|
|
"qemu": {
|
|
|
|
input: "qemu",
|
|
|
|
want: QEMU,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
p := FromString(tc.input)
|
|
|
|
|
|
|
|
assert.Equal(tc.want, p)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|