Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NEG mocks for alpha API and a new transactions test #955

Merged
merged 5 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions pkg/composite/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,14 @@ type HealthCheckReference struct {

// HealthStatusForNetworkEndpoint is a composite type wrapping the Alpha, Beta, and GA methods for its GCE equivalent
type HealthStatusForNetworkEndpoint struct {
// Version keeps track of the intended compute version for this HealthStatusForNetworkEndpoint.
// Note that the compute API's do not contain this field. It is for our
// own bookkeeping purposes.
Version meta.Version `json:"-"`
// Scope keeps track of the intended type of the service (e.g. Global)
// This is also an internal field purely for bookkeeping purposes
Scope meta.KeyType `json:"-"`

// URL of the backend service associated with the health state of the
// network endpoint.
BackendService *BackendServiceReference `json:"backendService,omitempty"`
Expand Down Expand Up @@ -1702,6 +1710,14 @@ type MutualTls struct {

// NetworkEndpoint is a composite type wrapping the Alpha, Beta, and GA methods for its GCE equivalent
type NetworkEndpoint struct {
// Version keeps track of the intended compute version for this NetworkEndpoint.
// Note that the compute API's do not contain this field. It is for our
// own bookkeeping purposes.
Version meta.Version `json:"-"`
// Scope keeps track of the intended type of the service (e.g. Global)
// This is also an internal field purely for bookkeeping purposes
Scope meta.KeyType `json:"-"`

// Metadata defined as annotations on the network endpoint.
Annotations map[string]string `json:"annotations,omitempty"`
// Optional fully qualified domain name of network endpoint. This can
Expand Down Expand Up @@ -3507,6 +3523,126 @@ func (healthCheck *HealthCheck) ToGA() (*compute.HealthCheck, error) {
return ga, nil
}

// ToHealthStatusForNetworkEndpointList converts a list of compute alpha, beta or GA
// HealthStatusForNetworkEndpoint into a list of our composite type.
func ToHealthStatusForNetworkEndpointList(objs interface{}) ([]*HealthStatusForNetworkEndpoint, error) {
result := []*HealthStatusForNetworkEndpoint{}

err := copyViaJSON(&result, objs)
if err != nil {
return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err)
}
return result, nil
}

// ToHealthStatusForNetworkEndpoint converts a compute alpha, beta or GA
// HealthStatusForNetworkEndpoint into our composite type.
func ToHealthStatusForNetworkEndpoint(obj interface{}) (*HealthStatusForNetworkEndpoint, error) {
healthStatusForNetworkEndpoint := &HealthStatusForNetworkEndpoint{}
err := copyViaJSON(healthStatusForNetworkEndpoint, obj)
if err != nil {
return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, healthStatusForNetworkEndpoint, err)
}

return healthStatusForNetworkEndpoint, nil
}

// ToAlpha converts our composite type into an alpha type.
// This alpha type can be used in GCE API calls.
func (healthStatusForNetworkEndpoint *HealthStatusForNetworkEndpoint) ToAlpha() (*computealpha.HealthStatusForNetworkEndpoint, error) {
alpha := &computealpha.HealthStatusForNetworkEndpoint{}
err := copyViaJSON(alpha, healthStatusForNetworkEndpoint)
if err != nil {
return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", healthStatusForNetworkEndpoint, err)
}

return alpha, nil
}

// ToBeta converts our composite type into an beta type.
// This beta type can be used in GCE API calls.
func (healthStatusForNetworkEndpoint *HealthStatusForNetworkEndpoint) ToBeta() (*computebeta.HealthStatusForNetworkEndpoint, error) {
beta := &computebeta.HealthStatusForNetworkEndpoint{}
err := copyViaJSON(beta, healthStatusForNetworkEndpoint)
if err != nil {
return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", healthStatusForNetworkEndpoint, err)
}

return beta, nil
}

// ToGA converts our composite type into an ga type.
// This ga type can be used in GCE API calls.
func (healthStatusForNetworkEndpoint *HealthStatusForNetworkEndpoint) ToGA() (*compute.HealthStatusForNetworkEndpoint, error) {
ga := &compute.HealthStatusForNetworkEndpoint{}
err := copyViaJSON(ga, healthStatusForNetworkEndpoint)
if err != nil {
return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", healthStatusForNetworkEndpoint, err)
}

return ga, nil
}

// ToNetworkEndpointList converts a list of compute alpha, beta or GA
// NetworkEndpoint into a list of our composite type.
func ToNetworkEndpointList(objs interface{}) ([]*NetworkEndpoint, error) {
result := []*NetworkEndpoint{}

err := copyViaJSON(&result, objs)
if err != nil {
return nil, fmt.Errorf("could not copy object %v to %T via JSON: %v", objs, result, err)
}
return result, nil
}

// ToNetworkEndpoint converts a compute alpha, beta or GA
// NetworkEndpoint into our composite type.
func ToNetworkEndpoint(obj interface{}) (*NetworkEndpoint, error) {
networkEndpoint := &NetworkEndpoint{}
err := copyViaJSON(networkEndpoint, obj)
if err != nil {
return nil, fmt.Errorf("could not copy object %+v to %T via JSON: %v", obj, networkEndpoint, err)
}

return networkEndpoint, nil
}

// ToAlpha converts our composite type into an alpha type.
// This alpha type can be used in GCE API calls.
func (networkEndpoint *NetworkEndpoint) ToAlpha() (*computealpha.NetworkEndpoint, error) {
alpha := &computealpha.NetworkEndpoint{}
err := copyViaJSON(alpha, networkEndpoint)
if err != nil {
return nil, fmt.Errorf("error converting %T to compute alpha type via JSON: %v", networkEndpoint, err)
}

return alpha, nil
}

// ToBeta converts our composite type into an beta type.
// This beta type can be used in GCE API calls.
func (networkEndpoint *NetworkEndpoint) ToBeta() (*computebeta.NetworkEndpoint, error) {
beta := &computebeta.NetworkEndpoint{}
err := copyViaJSON(beta, networkEndpoint)
if err != nil {
return nil, fmt.Errorf("error converting %T to compute beta type via JSON: %v", networkEndpoint, err)
}

return beta, nil
}

// ToGA converts our composite type into an ga type.
// This ga type can be used in GCE API calls.
func (networkEndpoint *NetworkEndpoint) ToGA() (*compute.NetworkEndpoint, error) {
ga := &compute.NetworkEndpoint{}
err := copyViaJSON(ga, networkEndpoint)
if err != nil {
return nil, fmt.Errorf("error converting %T to compute ga type via JSON: %v", networkEndpoint, err)
}

return ga, nil
}

func CreateNetworkEndpointGroup(gceCloud *gce.Cloud, key *meta.Key, networkEndpointGroup *NetworkEndpointGroup) error {
ctx, cancel := cloudprovider.ContextWithCallTimeout()
defer cancel()
Expand Down
190 changes: 186 additions & 4 deletions pkg/composite/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,104 @@ func TestHealthCheckReference(t *testing.T) {
t.Fatal(err)
}
}

func TestHealthStatusForNetworkEndpoint(t *testing.T) {
// Use reflection to verify that our composite type contains all the
// same fields as the alpha type.
compositeType := reflect.TypeOf(HealthStatusForNetworkEndpoint{})
alphaType := reflect.TypeOf(computealpha.HealthStatusForNetworkEndpoint{})
if err := typeEquality(compositeType, alphaType, true); err != nil {
betaType := reflect.TypeOf(computebeta.HealthStatusForNetworkEndpoint{})
gaType := reflect.TypeOf(compute.HealthStatusForNetworkEndpoint{})

// For the composite type, remove the Version field from consideration
compositeTypeNumFields := compositeType.NumField() - 2
if compositeTypeNumFields != alphaType.NumField() {
t.Fatalf("%v should contain %v fields. Got %v", alphaType.Name(), alphaType.NumField(), compositeTypeNumFields)
}

// Compare all the fields by doing a lookup since we can't guarantee that they'll be in the same order
// Make sure that composite type is strictly alpha fields + internal bookkeeping
for i := 2; i < compositeType.NumField(); i++ {
lookupField, found := alphaType.FieldByName(compositeType.Field(i).Name)
if !found {
t.Fatal(fmt.Errorf("Field %v not present in alpha type %v", compositeType.Field(i), alphaType))
}
if err := compareFields(compositeType.Field(i), lookupField); err != nil {
t.Fatal(err)
}
}

// Verify that all beta fields are in composite type
if err := typeEquality(betaType, compositeType, false); err != nil {
t.Fatal(err)
}

// Verify that all GA fields are in composite type
if err := typeEquality(gaType, compositeType, false); err != nil {
t.Fatal(err)
}
}

func TestToHealthStatusForNetworkEndpoint(t *testing.T) {
testCases := []struct {
input interface{}
expected *HealthStatusForNetworkEndpoint
}{
{
computealpha.HealthStatusForNetworkEndpoint{},
&HealthStatusForNetworkEndpoint{},
},
{
computebeta.HealthStatusForNetworkEndpoint{},
&HealthStatusForNetworkEndpoint{},
},
{
compute.HealthStatusForNetworkEndpoint{},
&HealthStatusForNetworkEndpoint{},
},
}
for _, testCase := range testCases {
result, _ := ToHealthStatusForNetworkEndpoint(testCase.input)
if !reflect.DeepEqual(result, testCase.expected) {
t.Fatalf("ToHealthStatusForNetworkEndpoint(input) = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(testCase.input), pretty.Sprint(result), pretty.Sprint(testCase.expected))
}
}
}

func TestHealthStatusForNetworkEndpointToAlpha(t *testing.T) {
composite := HealthStatusForNetworkEndpoint{}
expected := &computealpha.HealthStatusForNetworkEndpoint{}
result, err := composite.ToAlpha()
if err != nil {
t.Fatalf("HealthStatusForNetworkEndpoint.ToAlpha() error: %v", err)
}

if !reflect.DeepEqual(result, expected) {
t.Fatalf("HealthStatusForNetworkEndpoint.ToAlpha() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected))
}
}
func TestHealthStatusForNetworkEndpointToBeta(t *testing.T) {
composite := HealthStatusForNetworkEndpoint{}
expected := &computebeta.HealthStatusForNetworkEndpoint{}
result, err := composite.ToBeta()
if err != nil {
t.Fatalf("HealthStatusForNetworkEndpoint.ToBeta() error: %v", err)
}

if !reflect.DeepEqual(result, expected) {
t.Fatalf("HealthStatusForNetworkEndpoint.ToBeta() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected))
}
}
func TestHealthStatusForNetworkEndpointToGA(t *testing.T) {
composite := HealthStatusForNetworkEndpoint{}
expected := &compute.HealthStatusForNetworkEndpoint{}
result, err := composite.ToGA()
if err != nil {
t.Fatalf("HealthStatusForNetworkEndpoint.ToGA() error: %v", err)
}

if !reflect.DeepEqual(result, expected) {
t.Fatalf("HealthStatusForNetworkEndpoint.ToGA() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected))
}
}

func TestHostRule(t *testing.T) {
Expand Down Expand Up @@ -709,13 +800,104 @@ func TestMutualTls(t *testing.T) {
t.Fatal(err)
}
}

func TestNetworkEndpoint(t *testing.T) {
// Use reflection to verify that our composite type contains all the
// same fields as the alpha type.
compositeType := reflect.TypeOf(NetworkEndpoint{})
alphaType := reflect.TypeOf(computealpha.NetworkEndpoint{})
if err := typeEquality(compositeType, alphaType, true); err != nil {
betaType := reflect.TypeOf(computebeta.NetworkEndpoint{})
gaType := reflect.TypeOf(compute.NetworkEndpoint{})

// For the composite type, remove the Version field from consideration
compositeTypeNumFields := compositeType.NumField() - 2
if compositeTypeNumFields != alphaType.NumField() {
t.Fatalf("%v should contain %v fields. Got %v", alphaType.Name(), alphaType.NumField(), compositeTypeNumFields)
}

// Compare all the fields by doing a lookup since we can't guarantee that they'll be in the same order
// Make sure that composite type is strictly alpha fields + internal bookkeeping
for i := 2; i < compositeType.NumField(); i++ {
lookupField, found := alphaType.FieldByName(compositeType.Field(i).Name)
if !found {
t.Fatal(fmt.Errorf("Field %v not present in alpha type %v", compositeType.Field(i), alphaType))
}
if err := compareFields(compositeType.Field(i), lookupField); err != nil {
t.Fatal(err)
}
}

// Verify that all beta fields are in composite type
if err := typeEquality(betaType, compositeType, false); err != nil {
t.Fatal(err)
}

// Verify that all GA fields are in composite type
if err := typeEquality(gaType, compositeType, false); err != nil {
t.Fatal(err)
}
}

func TestToNetworkEndpoint(t *testing.T) {
testCases := []struct {
input interface{}
expected *NetworkEndpoint
}{
{
computealpha.NetworkEndpoint{},
&NetworkEndpoint{},
},
{
computebeta.NetworkEndpoint{},
&NetworkEndpoint{},
},
{
compute.NetworkEndpoint{},
&NetworkEndpoint{},
},
}
for _, testCase := range testCases {
result, _ := ToNetworkEndpoint(testCase.input)
if !reflect.DeepEqual(result, testCase.expected) {
t.Fatalf("ToNetworkEndpoint(input) = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(testCase.input), pretty.Sprint(result), pretty.Sprint(testCase.expected))
}
}
}

func TestNetworkEndpointToAlpha(t *testing.T) {
composite := NetworkEndpoint{}
expected := &computealpha.NetworkEndpoint{}
result, err := composite.ToAlpha()
if err != nil {
t.Fatalf("NetworkEndpoint.ToAlpha() error: %v", err)
}

if !reflect.DeepEqual(result, expected) {
t.Fatalf("NetworkEndpoint.ToAlpha() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected))
}
}
func TestNetworkEndpointToBeta(t *testing.T) {
composite := NetworkEndpoint{}
expected := &computebeta.NetworkEndpoint{}
result, err := composite.ToBeta()
if err != nil {
t.Fatalf("NetworkEndpoint.ToBeta() error: %v", err)
}

if !reflect.DeepEqual(result, expected) {
t.Fatalf("NetworkEndpoint.ToBeta() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected))
}
}
func TestNetworkEndpointToGA(t *testing.T) {
composite := NetworkEndpoint{}
expected := &compute.NetworkEndpoint{}
result, err := composite.ToGA()
if err != nil {
t.Fatalf("NetworkEndpoint.ToGA() error: %v", err)
}

if !reflect.DeepEqual(result, expected) {
t.Fatalf("NetworkEndpoint.ToGA() = \ninput = %s\n%s\nwant = \n%s", pretty.Sprint(composite), pretty.Sprint(result), pretty.Sprint(expected))
}
}
func TestNetworkEndpointGroup(t *testing.T) {
// Use reflection to verify that our composite type contains all the
Expand Down
Loading