mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-12-28 00:49:26 -05:00
2d8fcd9bf4
Co-authored-by: Malte Poll <mp@edgeless.systems> Co-authored-by: katexochen <katexochen@users.noreply.github.com> Co-authored-by: Daniel Weiße <dw@edgeless.systems> Co-authored-by: Thomas Tendyck <tt@edgeless.systems> Co-authored-by: Benedict Schlueter <bs@edgeless.systems> Co-authored-by: leongross <leon.gross@rub.de> Co-authored-by: Moritz Eckert <m1gh7ym0@gmail.com>
97 lines
2.2 KiB
Go
97 lines
2.2 KiB
Go
package gcp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/google/go-tpm-tools/proto/attest"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetGCEInstanceInfo(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
client fakeMetadataClient
|
|
errExpected bool
|
|
}{
|
|
"success": {
|
|
client: fakeMetadataClient{
|
|
projectIDString: "projectID",
|
|
instanceNameString: "instanceName",
|
|
zoneString: "zone",
|
|
},
|
|
errExpected: false,
|
|
},
|
|
"projectID error": {
|
|
client: fakeMetadataClient{
|
|
projectIDString: "projectID",
|
|
instanceNameString: "instanceName",
|
|
zoneString: "zone",
|
|
projecIdErr: errors.New("error"),
|
|
},
|
|
errExpected: true,
|
|
},
|
|
"instanceName error": {
|
|
client: fakeMetadataClient{
|
|
projectIDString: "projectID",
|
|
instanceNameString: "instanceName",
|
|
zoneString: "zone",
|
|
instanceNameErr: errors.New("error"),
|
|
},
|
|
errExpected: true,
|
|
},
|
|
"zone error": {
|
|
client: fakeMetadataClient{
|
|
projectIDString: "projectID",
|
|
instanceNameString: "instanceName",
|
|
zoneString: "zone",
|
|
zoneErr: errors.New("error"),
|
|
},
|
|
errExpected: true,
|
|
},
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
require := require.New(t)
|
|
var tpm io.ReadWriteCloser
|
|
|
|
out, err := getGCEInstanceInfo(tc.client)(tpm)
|
|
if tc.errExpected {
|
|
assert.Error(err)
|
|
} else {
|
|
assert.NoError(err)
|
|
var info attest.GCEInstanceInfo
|
|
require.NoError(json.Unmarshal(out, &info))
|
|
assert.Equal(tc.client.projectIDString, info.ProjectId)
|
|
assert.Equal(tc.client.instanceNameString, info.InstanceName)
|
|
assert.Equal(tc.client.zoneString, info.Zone)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type fakeMetadataClient struct {
|
|
projectIDString string
|
|
instanceNameString string
|
|
zoneString string
|
|
projecIdErr error
|
|
instanceNameErr error
|
|
zoneErr error
|
|
}
|
|
|
|
func (c fakeMetadataClient) projectID() (string, error) {
|
|
return c.projectIDString, c.projecIdErr
|
|
}
|
|
|
|
func (c fakeMetadataClient) instanceName() (string, error) {
|
|
return c.instanceNameString, c.instanceNameErr
|
|
}
|
|
|
|
func (c fakeMetadataClient) zone() (string, error) {
|
|
return c.zoneString, c.zoneErr
|
|
}
|