-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #844 from richardcase/gke_pr6_e2e
test: adding e2e tests for gke (GKE Part 6)
- Loading branch information
Showing
34 changed files
with
1,118 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package providerid implements functionality for creating kubernetes provider ids for nodes. | ||
package providerid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package providerid | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"path" | ||
|
||
"sigs.k8s.io/cluster-api-provider-gcp/util/resourceurl" | ||
) | ||
|
||
const ( | ||
// Prefix is the gce provider id prefix. | ||
Prefix = "gce://" | ||
) | ||
|
||
// ProviderID represents the id for a GCP cluster. | ||
type ProviderID interface { | ||
Project() string | ||
Location() string | ||
Name() string | ||
fmt.Stringer | ||
} | ||
|
||
// NewFromResourceURL creates a provider from a GCP resource url. | ||
func NewFromResourceURL(url string) (ProviderID, error) { | ||
resourceURL, err := resourceurl.Parse(url) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing resource url %s: %w", url, err) | ||
} | ||
|
||
return New(resourceURL.Project, resourceURL.Location, resourceURL.Name) | ||
} | ||
|
||
// New creates a new provider id. | ||
func New(project, location, name string) (ProviderID, error) { | ||
if project == "" { | ||
return nil, errors.New("project required for provider id") | ||
} | ||
if location == "" { | ||
return nil, errors.New("location required for provider id") | ||
} | ||
if name == "" { | ||
return nil, errors.New("name required for provider id") | ||
} | ||
|
||
return &providerID{ | ||
project: project, | ||
location: location, | ||
name: name, | ||
}, nil | ||
} | ||
|
||
type providerID struct { | ||
project string | ||
location string | ||
name string | ||
} | ||
|
||
func (p *providerID) Project() string { | ||
return p.project | ||
} | ||
|
||
func (p *providerID) Location() string { | ||
return p.location | ||
} | ||
|
||
func (p *providerID) Name() string { | ||
return p.name | ||
} | ||
|
||
func (p *providerID) String() string { | ||
return Prefix + path.Join(p.project, p.location, p.name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package providerid_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/cluster-api-provider-gcp/cloud/providerid" | ||
) | ||
|
||
func TestProviderID_New(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
testCases := []struct { | ||
testname string | ||
project string | ||
location string | ||
name string | ||
expectedProviderID string | ||
expectError bool | ||
}{ | ||
{ | ||
testname: "no project, should fail", | ||
project: "", | ||
location: "eu-west4", | ||
name: "vm1", | ||
expectError: true, | ||
}, | ||
{ | ||
testname: "no location, should fail", | ||
project: "proj1", | ||
location: "", | ||
name: "vm1", | ||
expectError: true, | ||
}, | ||
{ | ||
testname: "no name, should fail", | ||
project: "proj1", | ||
location: "eu-west4", | ||
name: "", | ||
expectError: true, | ||
}, | ||
{ | ||
testname: "with all details, should pass", | ||
project: "proj1", | ||
location: "eu-west4", | ||
name: "vm1", | ||
expectError: false, | ||
expectedProviderID: "gce://proj1/eu-west4/vm1", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.testname, func(t *testing.T) { | ||
providerID, err := providerid.New(tc.project, tc.location, tc.name) | ||
|
||
if tc.expectError { | ||
Expect(err).To(HaveOccurred()) | ||
} else { | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(providerID.String()).To(Equal(tc.expectedProviderID)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestProviderID_NewFromResourceURL(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
testCases := []struct { | ||
testname string | ||
resourceURL string | ||
expectedProviderID string | ||
expectError bool | ||
}{ | ||
{ | ||
testname: "invalid url, should fail", | ||
resourceURL: "hvfnhdkdk", | ||
expectError: true, | ||
}, | ||
{ | ||
testname: "valid instance url, should pass", | ||
resourceURL: "https://www.googleapis.com/compute/v1/projects/myproject/zones/europe-west2-a/instances/gke-capg-dskczmdculd-capg-e2e-ebs0oy--014f89ba-sx2p", | ||
expectError: false, | ||
expectedProviderID: "gce://myproject/europe-west2-a/gke-capg-dskczmdculd-capg-e2e-ebs0oy--014f89ba-sx2p", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.testname, func(t *testing.T) { | ||
providerID, err := providerid.NewFromResourceURL(tc.resourceURL) | ||
|
||
if tc.expectError { | ||
Expect(err).To(HaveOccurred()) | ||
} else { | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(providerID.String()).To(Equal(tc.expectedProviderID)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package clusters | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
// ErrAutopilotClusterMachinePoolsNotAllowed is used when there are machine pools specified for an autopilot enabled cluster. | ||
ErrAutopilotClusterMachinePoolsNotAllowed = errors.New("cannot use machine pools with an autopilot enabled cluster") | ||
) | ||
|
||
// NewErrUnexpectedClusterStatus creates a new error for an unexpected cluster status. | ||
func NewErrUnexpectedClusterStatus(status string) error { | ||
return &errUnexpectedClusterStatus{status} | ||
} | ||
|
||
type errUnexpectedClusterStatus struct { | ||
status string | ||
} | ||
|
||
func (e *errUnexpectedClusterStatus) Error() string { | ||
return fmt.Sprintf("unexpected error status: %s", e.status) | ||
} |
Oops, something went wrong.