2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
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 {
|
2022-04-26 10:54:05 -04:00
|
|
|
client fakeMetadataClient
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
client: fakeMetadataClient{
|
|
|
|
projectIDString: "projectID",
|
|
|
|
instanceNameString: "instanceName",
|
|
|
|
zoneString: "zone",
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"projectID error": {
|
|
|
|
client: fakeMetadataClient{
|
|
|
|
projectIDString: "projectID",
|
|
|
|
instanceNameString: "instanceName",
|
|
|
|
zoneString: "zone",
|
2022-07-08 04:59:59 -04:00
|
|
|
projecIDErr: errors.New("error"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"instanceName error": {
|
|
|
|
client: fakeMetadataClient{
|
|
|
|
projectIDString: "projectID",
|
|
|
|
instanceNameString: "instanceName",
|
|
|
|
zoneString: "zone",
|
|
|
|
instanceNameErr: errors.New("error"),
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"zone error": {
|
|
|
|
client: fakeMetadataClient{
|
|
|
|
projectIDString: "projectID",
|
|
|
|
instanceNameString: "instanceName",
|
|
|
|
zoneString: "zone",
|
|
|
|
zoneErr: errors.New("error"),
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
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
|
2022-07-08 04:59:59 -04:00
|
|
|
projecIDErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
instanceNameErr error
|
|
|
|
zoneErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c fakeMetadataClient) projectID() (string, error) {
|
2022-07-08 04:59:59 -04:00
|
|
|
return c.projectIDString, c.projecIDErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c fakeMetadataClient) instanceName() (string, error) {
|
|
|
|
return c.instanceNameString, c.instanceNameErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c fakeMetadataClient) zone() (string, error) {
|
|
|
|
return c.zoneString, c.zoneErr
|
|
|
|
}
|