From 4ae2c3a10103be200157c62c01f8a0fe272df063 Mon Sep 17 00:00:00 2001 From: Sergiy Markin Date: Tue, 26 Mar 2024 16:35:37 +0000 Subject: [PATCH] Fixed lint and unit tests This PS makes sure we have linter and unit tests processed. The code has been reformatted to adhere to Go's code formatting conventions. Change-Id: I31f15d6d6c4b9bda7e3837941b6c9c3c3735aea7 --- Makefile | 25 +++++-- client/client.go | 6 +- dependencies/config/config.go | 8 +-- dependencies/config/config_test.go | 15 +++-- dependencies/container/container.go | 2 - dependencies/container/container_test.go | 1 - dependencies/daemonset/daemonset.go | 1 - dependencies/daemonset/daemonset_test.go | 2 - dependencies/job/job_test.go | 8 +-- dependencies/pod/pod_test.go | 7 +- dependencies/service/service.go | 1 - dependencies/service/service_test.go | 7 +- dependencies/socket/socket_test.go | 1 - entrypoint/entrypoint.go | 4 +- entrypoint/entrypoint_test.go | 8 ++- logger/logger.go | 6 +- mocks/client.go | 5 +- mocks/daemonset.go | 54 ++++++++++++--- mocks/endpoints.go | 58 ++++++++++++---- mocks/job.go | 48 +++++++++++--- mocks/pod.go | 66 +++++++++++++++---- mocks/service.go | 53 ++++++++++++--- .../airship-kubernetes-entrypoint-lint.yaml | 20 ++++++ ...> airship-kubernetes-entrypoint-unit.yaml} | 9 +-- tools/tools.go | 1 + util/util.go | 2 +- util/util_test.go | 1 - zuul.d/jobs.yaml | 15 +++++ zuul.d/projects.yaml | 7 +- 29 files changed, 330 insertions(+), 111 deletions(-) create mode 100644 playbooks/airship-kubernetes-entrypoint-lint.yaml rename playbooks/{airship-kubernetes-entrypoint-lint-unit.yaml => airship-kubernetes-entrypoint-unit.yaml} (76%) diff --git a/Makefile b/Makefile index 3aa1ef0..d9bce28 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,8 @@ endif _BASE_IMAGE_ARG := $(if $(UBUNTU_BASE_IMAGE),--build-arg FROM="${UBUNTU_BASE_IMAGE}" ,) +MAKE_TARGET := build + # CONTAINER_TOOL defines the container tool to be used for building images. # Be aware that the target commands are only tested with Docker which is # scaffolded by default. However, you might want to replace it to use other @@ -69,7 +71,7 @@ PKG := ./... TESTS := . .PHONY: all -all: build +all: build unit-tests .PHONY: build build: @@ -77,9 +79,17 @@ build: @CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -o bin/kubernetes-entrypoint .PHONY: lint -lint: +lint: build @go run ${LINTER_CMD} --config ${LINTER_CONFIG} +.PHONY: fmt +fmt: ## Run go fmt against code. + go fmt ./... + +.PHONY: vet +vet: ## Run go vet against code. + go vet ./... + .PHONY: docker-image docker-image: ifeq ($(USE_PROXY), true) @@ -89,6 +99,7 @@ ifeq ($(USE_PROXY), true) --label "org.opencontainers.image.title=$(IMAGE_NAME)" \ -f images/Dockerfile.$(DISTRO) \ $(_BASE_IMAGE_ARG) \ + --build-arg MAKE_TARGET=$(MAKE_TARGET) \ --build-arg http_proxy=$(PROXY) \ --build-arg https_proxy=$(PROXY) \ --build-arg HTTP_PROXY=$(PROXY) \ @@ -101,6 +112,7 @@ else --label "org.opencontainers.image.created=$(shell date --rfc-3339=seconds --utc)" \ --label "org.opencontainers.image.title=$(IMAGE_NAME)" \ -f images/Dockerfile.$(DISTRO) \ + --build-arg MAKE_TARGET=$(MAKE_TARGET) \ $(_BASE_IMAGE_ARG) . endif ifeq ($(PUSH_IMAGE), true) @@ -117,15 +129,14 @@ check-docker: images: check-docker docker-image .PHONY: docker-image-unit-tests -docker-image-unit-tests: DOCKER_MAKE_TARGET = unit-tests -docker-image-unit-tests: DOCKER_TARGET_STAGE = builder +docker-image-unit-tests: MAKE_TARGET = unit-tests docker-image-unit-tests: docker-image .PHONY: docker-image-lint -docker-image-lint: DOCKER_MAKE_TARGET = lint -docker-image-lint: DOCKER_TARGET_STAGE = builder +docker-image-lint: MAKE_TARGET = lint docker-image-lint: docker-image + .PHONY: get-modules get-modules: @go mod download @@ -135,5 +146,5 @@ clean: @rm -rf bin .PHONY: unit-test -unit-tests: +unit-tests: build @go test -v ./... diff --git a/client/client.go b/client/client.go index 9b7c0a8..b10f47d 100644 --- a/client/client.go +++ b/client/client.go @@ -40,6 +40,7 @@ func (c Client) Jobs(namespace string) v1batch.JobInterface { func (c Client) Endpoints(namespace string) v1core.EndpointsInterface { return c.client.CoreV1().Endpoints(namespace) } + func (c Client) DaemonSets(namespace string) appsv1.DaemonSetInterface { return c.client.AppsV1().DaemonSets(namespace) } @@ -48,7 +49,10 @@ func (c Client) Services(namespace string) v1core.ServiceInterface { return c.client.CoreV1().Services(namespace) } -func (c Client) CustomResource(ctx context.Context, apiVersion, kind, namespace, name string) (*unstructured.Unstructured, error) { +func (c Client) CustomResource( + ctx context.Context, + apiVersion, kind, namespace, name string, +) (*unstructured.Unstructured, error) { apiResourceList, err := c.client.Discovery().ServerResourcesForGroupVersion(apiVersion) if err != nil { return nil, err diff --git a/dependencies/config/config.go b/dependencies/config/config.go index 6f1f816..f1c77a6 100644 --- a/dependencies/config/config.go +++ b/dependencies/config/config.go @@ -64,13 +64,14 @@ func NewConfig(name string, prefix string) (*Config, error) { params: configParams{ IP: ip, IP_ERLANG: strings.Replace(ip, ".", ",", -1), - HOSTNAME: hostname}, + HOSTNAME: hostname, + }, prefix: prefix, }, nil } func (c Config) IsResolved(ctx context.Context, entrypoint entry.EntrypointInterface) (bool, error) { - //Create directory to ensure it exists + // Create directory to ensure it exists if err := createDirectory(c.name); err != nil { return false, fmt.Errorf("couldn't create directory: %v", err) } @@ -78,7 +79,6 @@ func (c Config) IsResolved(ctx context.Context, entrypoint entry.EntrypointInter return false, fmt.Errorf("cannot template %s: %v", c.name, err) } return true, nil - } func (c Config) createAndTemplateConfig() error { @@ -100,7 +100,7 @@ func getSrcConfig(prefix string, config string) (srcConfig string) { } func createDirectory(file string) error { - return os.MkdirAll(filepath.Dir(file), 0755) + return os.MkdirAll(filepath.Dir(file), 0o755) } func (c Config) String() string { diff --git a/dependencies/config/config_test.go b/dependencies/config/config_test.go index 2afdf45..58ba652 100644 --- a/dependencies/config/config_test.go +++ b/dependencies/config/config_test.go @@ -24,10 +24,12 @@ const ( templatePrefix = "/tmp/templates" ) -var testEntrypoint entrypoint.EntrypointInterface -var testConfigContents string -var testConfigPath string -var testTemplatePath string +var ( + testEntrypoint entrypoint.EntrypointInterface + testConfigContents string + testConfigPath string + testTemplatePath string +) // var testClient cli.ClientInterface @@ -54,11 +56,11 @@ func teardownOsEnvironment() (err error) { func setupConfigTemplate(templatePath string) error { configContent := []byte(testConfigContents) - if err := os.MkdirAll(filepath.Dir(templatePath), 0755); err != nil { + if err := os.MkdirAll(filepath.Dir(templatePath), 0o755); err != nil { return err } - if err := os.WriteFile(templatePath, configContent, 0644); err != nil { + if err := os.WriteFile(templatePath, configContent, 0o644); err != nil { return err } @@ -78,7 +80,6 @@ func teardownConfigTemplate() (err error) { } var _ = Describe("Config", func() { - BeforeEach(func() { err := setupOsEnvironment() Expect(err).NotTo(HaveOccurred()) diff --git a/dependencies/container/container.go b/dependencies/container/container.go index b1b7d1a..43224b7 100644 --- a/dependencies/container/container.go +++ b/dependencies/container/container.go @@ -30,7 +30,6 @@ func init() { os.Exit(1) } if containerDeps := env.SplitEnvToDeps(containerEnv); containerDeps != nil { - if len(containerDeps) > 0 { for _, dep := range containerDeps { entry.Register(NewContainer(dep.Name)) @@ -41,7 +40,6 @@ func init() { func NewContainer(name string) Container { return Container{name: name} - } func (c Container) IsResolved(ctx context.Context, entrypoint entry.EntrypointInterface) (bool, error) { diff --git a/dependencies/container/container_test.go b/dependencies/container/container_test.go index c312fe0..aca0281 100644 --- a/dependencies/container/container_test.go +++ b/dependencies/container/container_test.go @@ -19,7 +19,6 @@ const ( var testEntrypoint entrypoint.EntrypointInterface var _ = Describe("Container", func() { - BeforeEach(func() { err := os.Setenv(podEnvVariableName, mocks.PodEnvVariableValue) Expect(err).NotTo(HaveOccurred()) diff --git a/dependencies/daemonset/daemonset.go b/dependencies/daemonset/daemonset.go index 1ee8e63..21f3b17 100644 --- a/dependencies/daemonset/daemonset.go +++ b/dependencies/daemonset/daemonset.go @@ -51,7 +51,6 @@ func NewDaemonset(name string, namespace string) (*Daemonset, error) { func (d Daemonset) IsResolved(ctx context.Context, entrypoint entry.EntrypointInterface) (bool, error) { var myPodName string daemonset, err := entrypoint.Client().DaemonSets(d.namespace).Get(ctx, d.name, metav1.GetOptions{}) - if err != nil { return false, err } diff --git a/dependencies/daemonset/daemonset_test.go b/dependencies/daemonset/daemonset_test.go index c7f64b6..3ce5930 100644 --- a/dependencies/daemonset/daemonset_test.go +++ b/dependencies/daemonset/daemonset_test.go @@ -20,7 +20,6 @@ const ( var testEntrypoint entrypoint.EntrypointInterface var _ = Describe("Daemonset", func() { - BeforeEach(func() { err := os.Setenv(PodNameEnvVar, podEnvVariableValue) Expect(err).NotTo(HaveOccurred()) @@ -102,7 +101,6 @@ var _ = Describe("Daemonset", func() { Expect(isResolved).To(BeTrue()) Expect(err).NotTo(HaveOccurred()) - }) It("checks resolution of an incorrect daemonset namespace", func() { diff --git a/dependencies/job/job_test.go b/dependencies/job/job_test.go index e7c3ce1..68b3150 100644 --- a/dependencies/job/job_test.go +++ b/dependencies/job/job_test.go @@ -11,8 +11,10 @@ import ( "opendev.org/airship/kubernetes-entrypoint/mocks" ) -const testJobName = "TEST_JOB_NAME" -const testJobNamespace = "TEST_JOB_NAMESPACE" +const ( + testJobName = "TEST_JOB_NAME" + testJobNamespace = "TEST_JOB_NAMESPACE" +) var testLabels = map[string]string{ "k1": "v1", @@ -21,7 +23,6 @@ var testLabels = map[string]string{ var testEntrypoint entrypoint.EntrypointInterface var _ = Describe("Job", func() { - BeforeEach(func() { testEntrypoint = mocks.NewEntrypoint() }) @@ -78,5 +79,4 @@ var _ = Describe("Job", func() { Expect(isResolved).To(Equal(false)) Expect(err.Error()).To(Equal(fmt.Sprintf(FailingStatusFormat, job))) }) - }) diff --git a/dependencies/pod/pod_test.go b/dependencies/pod/pod_test.go index 7a08aa7..2a90199 100644 --- a/dependencies/pod/pod_test.go +++ b/dependencies/pod/pod_test.go @@ -18,11 +18,12 @@ const ( requireSameNode = true ) -var testEntrypoint entrypoint.EntrypointInterface -var testLabels = map[string]string{"foo": "bar"} +var ( + testEntrypoint entrypoint.EntrypointInterface + testLabels = map[string]string{"foo": "bar"} +) var _ = Describe("Pod", func() { - BeforeEach(func() { err := os.Setenv(PodNameEnvVar, podEnvVariableValue) Expect(err).NotTo(HaveOccurred()) diff --git a/dependencies/service/service.go b/dependencies/service/service.go index 1af073b..3a6b61d 100644 --- a/dependencies/service/service.go +++ b/dependencies/service/service.go @@ -33,7 +33,6 @@ func NewService(name string, namespace string) Service { name: name, namespace: namespace, } - } func (s Service) IsResolved(ctx context.Context, entrypoint entry.EntrypointInterface) (bool, error) { diff --git a/dependencies/service/service_test.go b/dependencies/service/service_test.go index ff4b029..45c1d70 100644 --- a/dependencies/service/service_test.go +++ b/dependencies/service/service_test.go @@ -11,13 +11,14 @@ import ( "opendev.org/airship/kubernetes-entrypoint/mocks" ) -const testServiceName = "TEST_SERVICE_NAME" -const testServiceNamespace = "TEST_SERVICE_NAMESPACE" +const ( + testServiceName = "TEST_SERVICE_NAME" + testServiceNamespace = "TEST_SERVICE_NAMESPACE" +) var testEntrypoint entrypoint.EntrypointInterface var _ = Describe("Service", func() { - BeforeEach(func() { testEntrypoint = mocks.NewEntrypoint() }) diff --git a/dependencies/socket/socket_test.go b/dependencies/socket/socket_test.go index df93785..e5e8506 100644 --- a/dependencies/socket/socket_test.go +++ b/dependencies/socket/socket_test.go @@ -30,7 +30,6 @@ var ( var testEntrypoint entrypoint.EntrypointInterface var _ = Describe("Socket", func() { - // NOTE: It is impossible for a user to create a file that he does not // have access to, and thus it is impossible to write an isolated unit // test that checks for permission errors. That test is omitted from diff --git a/entrypoint/entrypoint.go b/entrypoint/entrypoint.go index 90a5fe7..20bd227 100644 --- a/entrypoint/entrypoint.go +++ b/entrypoint/entrypoint.go @@ -13,7 +13,7 @@ import ( var dependencies []Resolver // List containing all dependencies to be resolved const ( - //DependencyPrefix is a prefix for env variables + // DependencyPrefix is a prefix for env variables DependencyPrefix = "DEPENDENCY_" JsonSuffix = "_JSON" resolverSleepInterval = 2 @@ -76,9 +76,7 @@ func (e Entrypoint) Resolve() { time.Sleep(resolverSleepInterval * time.Second) } logger.Info.Printf("Dependency %v is resolved.", dep) - }(dep) } wg.Wait() - } diff --git a/entrypoint/entrypoint_test.go b/entrypoint/entrypoint_test.go index d090e26..016e30a 100644 --- a/entrypoint/entrypoint_test.go +++ b/entrypoint/entrypoint_test.go @@ -21,8 +21,10 @@ const ( loggerInfoText = "Entrypoint INFO: " ) -var testEntrypoint EntrypointInterface -var testClient cli.ClientInterface +var ( + testEntrypoint EntrypointInterface + testClient cli.ClientInterface +) type dummyResolver struct { name string @@ -32,6 +34,7 @@ type dummyResolver struct { func (d dummyResolver) IsResolved(ctx context.Context, entry EntrypointInterface) (bool, error) { return true, nil } + func (d dummyResolver) GetName() (name string) { return d.name } @@ -50,7 +53,6 @@ func registerNilResolver() { } var _ = Describe("Entrypoint", func() { - dummy := dummyResolver{name: dummyResolverName} BeforeEach(func() { diff --git a/logger/logger.go b/logger/logger.go index ddcb006..5819c11 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -6,11 +6,11 @@ import ( ) var ( - //Info logger + // Info logger Info *log.Logger - //Error logger + // Error logger Error *log.Logger - //Warning logger + // Warning logger Warning *log.Logger ) diff --git a/mocks/client.go b/mocks/client.go index 7da7c7d..b95a078 100644 --- a/mocks/client.go +++ b/mocks/client.go @@ -40,7 +40,10 @@ func (c Client) Jobs(namespace string) v1batch.JobInterface { return c.JobInterface } -func (c Client) CustomResource(ctx context.Context, apiVersion, namespace, resource, name string) (*unstructured.Unstructured, error) { +func (c Client) CustomResource( + ctx context.Context, + apiVersion, namespace, resource, name string, +) (*unstructured.Unstructured, error) { return c.FakeCustomResource, c.Err } diff --git a/mocks/daemonset.go b/mocks/daemonset.go index 1127aea..f238ddb 100644 --- a/mocks/daemonset.go +++ b/mocks/daemonset.go @@ -12,8 +12,7 @@ import ( appsv1 "k8s.io/client-go/kubernetes/typed/apps/v1" ) -type dClient struct { -} +type dClient struct{} const ( SucceedingDaemonsetName = "DAEMONSET_SUCCEED" @@ -26,15 +25,27 @@ const ( NotReadyMatchLabelsDaemonsetName = "DAEMONSET_NOT_READY_MATCH_LABELS" ) -func (d dClient) Create(ctx context.Context, daemonSet *v1.DaemonSet, opts metav1.CreateOptions) (*v1.DaemonSet, error) { +func (d dClient) Create( + ctx context.Context, + daemonSet *v1.DaemonSet, + opts metav1.CreateOptions, +) (*v1.DaemonSet, error) { return nil, fmt.Errorf("not implemented") } -func (d dClient) Update(ctx context.Context, daemonSet *v1.DaemonSet, opts metav1.UpdateOptions) (*v1.DaemonSet, error) { +func (d dClient) Update( + ctx context.Context, + daemonSet *v1.DaemonSet, + opts metav1.UpdateOptions, +) (*v1.DaemonSet, error) { return nil, fmt.Errorf("not implemented") } -func (d dClient) UpdateStatus(ctx context.Context, daemonSet *v1.DaemonSet, opts metav1.UpdateOptions) (*v1.DaemonSet, error) { +func (d dClient) UpdateStatus( + ctx context.Context, + daemonSet *v1.DaemonSet, + opts metav1.UpdateOptions, +) (*v1.DaemonSet, error) { return nil, fmt.Errorf("not implemented") } @@ -42,11 +53,19 @@ func (d dClient) Delete(ctx context.Context, name string, opts metav1.DeleteOpti return fmt.Errorf("not implemented") } -func (d dClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error { +func (d dClient) DeleteCollection( + ctx context.Context, + opts metav1.DeleteOptions, + listOpts metav1.ListOptions, +) error { return fmt.Errorf("not implemented") } -func (d dClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*v1.DaemonSet, error) { +func (d dClient) Get( + ctx context.Context, + name string, + opts metav1.GetOptions, +) (*v1.DaemonSet, error) { if name == FailingDaemonsetName || name == IncorrectNamespaceDaemonsetName { return nil, fmt.Errorf("mock daemonset didn't work") } @@ -83,15 +102,30 @@ func (d dClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Inte return nil, fmt.Errorf("not implemented") } -func (d dClient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *v1.DaemonSet, err error) { +func (d dClient) Patch( + ctx context.Context, + name string, + pt types.PatchType, + data []byte, + opts metav1.PatchOptions, + subresources ...string, +) (result *v1.DaemonSet, err error) { return nil, fmt.Errorf("not implemented") } -func (d dClient) Apply(ctx context.Context, daemonSet *appsv1applyconfigurations.DaemonSetApplyConfiguration, opts metav1.ApplyOptions) (result *v1.DaemonSet, err error) { +func (d dClient) Apply( + ctx context.Context, + daemonSet *appsv1applyconfigurations.DaemonSetApplyConfiguration, + opts metav1.ApplyOptions, +) (result *v1.DaemonSet, err error) { return nil, fmt.Errorf("not implemented") } -func (d dClient) ApplyStatus(ctx context.Context, daemonSet *appsv1applyconfigurations.DaemonSetApplyConfiguration, opts metav1.ApplyOptions) (result *v1.DaemonSet, err error) { +func (d dClient) ApplyStatus( + ctx context.Context, + daemonSet *appsv1applyconfigurations.DaemonSetApplyConfiguration, + opts metav1.ApplyOptions, +) (result *v1.DaemonSet, err error) { return nil, fmt.Errorf("not implemented") } diff --git a/mocks/endpoints.go b/mocks/endpoints.go index 6acf52c..bfa7082 100644 --- a/mocks/endpoints.go +++ b/mocks/endpoints.go @@ -12,30 +12,49 @@ import ( corev1 "k8s.io/client-go/kubernetes/typed/core/v1" ) -type eClient struct { -} +type eClient struct{} const ( MockEndpointError = "mock endpoint didnt work" ) -func (e eClient) Create(ctx context.Context, endpoints *v1.Endpoints, opts metav1.CreateOptions) (*v1.Endpoints, error) { +func (e eClient) Create( + ctx context.Context, + endpoints *v1.Endpoints, + opts metav1.CreateOptions, +) (*v1.Endpoints, error) { return nil, fmt.Errorf("not implemented") } -func (e eClient) Update(ctx context.Context, endpoints *v1.Endpoints, opts metav1.UpdateOptions) (*v1.Endpoints, error) { +func (e eClient) Update( + ctx context.Context, + endpoints *v1.Endpoints, + opts metav1.UpdateOptions, +) (*v1.Endpoints, error) { return nil, fmt.Errorf("not implemented") } -func (e eClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error { +func (e eClient) Delete( + ctx context.Context, + name string, + opts metav1.DeleteOptions, +) error { return fmt.Errorf("not implemented") } -func (e eClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error { +func (e eClient) DeleteCollection( + ctx context.Context, + opts metav1.DeleteOptions, + listOpts metav1.ListOptions, +) error { return fmt.Errorf("not implemented") } -func (e eClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Endpoints, error) { +func (e eClient) Get( + ctx context.Context, + name string, + opts metav1.GetOptions, +) (*v1.Endpoints, error) { if name == FailingServiceName { return nil, fmt.Errorf(MockEndpointError) } @@ -60,19 +79,36 @@ func (e eClient) Get(ctx context.Context, name string, opts metav1.GetOptions) ( return endpoint, nil } -func (e eClient) List(ctx context.Context, opts metav1.ListOptions) (*v1.EndpointsList, error) { +func (e eClient) List( + ctx context.Context, + opts metav1.ListOptions, +) (*v1.EndpointsList, error) { return nil, fmt.Errorf("not implemented") } -func (e eClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { +func (e eClient) Watch( + ctx context.Context, + opts metav1.ListOptions, +) (watch.Interface, error) { return nil, fmt.Errorf("not implemented") } -func (e eClient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *v1.Endpoints, err error) { +func (e eClient) Patch( + ctx context.Context, + name string, + pt types.PatchType, + data []byte, + opts metav1.PatchOptions, + subresources ...string, +) (result *v1.Endpoints, err error) { return nil, fmt.Errorf("not implemented") } -func (e eClient) Apply(ctx context.Context, endpoints *corev1applyconfigurations.EndpointsApplyConfiguration, opts metav1.ApplyOptions) (result *v1.Endpoints, err error) { +func (e eClient) Apply( + ctx context.Context, + endpoints *corev1applyconfigurations.EndpointsApplyConfiguration, + opts metav1.ApplyOptions, +) (result *v1.Endpoints, err error) { return nil, fmt.Errorf("not implemented") } diff --git a/mocks/job.go b/mocks/job.go index 8baeb19..7f269b2 100644 --- a/mocks/job.go +++ b/mocks/job.go @@ -19,18 +19,29 @@ const ( FailingJobLabel = "fail" ) -type jClient struct { -} +type jClient struct{} -func (j jClient) Create(ctx context.Context, job *v1.Job, opts metav1.CreateOptions) (*v1.Job, error) { +func (j jClient) Create( + ctx context.Context, + job *v1.Job, + opts metav1.CreateOptions, +) (*v1.Job, error) { return nil, fmt.Errorf("not implemented") } -func (j jClient) Update(ctx context.Context, job *v1.Job, opts metav1.UpdateOptions) (*v1.Job, error) { +func (j jClient) Update( + ctx context.Context, + job *v1.Job, + opts metav1.UpdateOptions, +) (*v1.Job, error) { return nil, fmt.Errorf("not implemented") } -func (j jClient) UpdateStatus(ctx context.Context, job *v1.Job, opts metav1.UpdateOptions) (*v1.Job, error) { +func (j jClient) UpdateStatus( + ctx context.Context, + job *v1.Job, + opts metav1.UpdateOptions, +) (*v1.Job, error) { return nil, fmt.Errorf("not implemented") } @@ -38,7 +49,11 @@ func (j jClient) Delete(ctx context.Context, name string, opts metav1.DeleteOpti return fmt.Errorf("not implemented") } -func (j jClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error { +func (j jClient) DeleteCollection( + ctx context.Context, + opts metav1.DeleteOptions, + listOpts metav1.ListOptions, +) error { return fmt.Errorf("not implemented") } @@ -77,15 +92,30 @@ func (j jClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Inte return nil, fmt.Errorf("not implemented") } -func (j jClient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *v1.Job, err error) { +func (j jClient) Patch( + ctx context.Context, + name string, + pt types.PatchType, + data []byte, + opts metav1.PatchOptions, + subresources ...string, +) (result *v1.Job, err error) { return nil, fmt.Errorf("not implemented") } -func (j jClient) Apply(ctx context.Context, job *batchv1applyconfigurations.JobApplyConfiguration, opts metav1.ApplyOptions) (result *v1.Job, err error) { +func (j jClient) Apply( + ctx context.Context, + job *batchv1applyconfigurations.JobApplyConfiguration, + opts metav1.ApplyOptions, +) (result *v1.Job, err error) { return nil, fmt.Errorf("not implemented") } -func (j jClient) ApplyStatus(ctx context.Context, job *batchv1applyconfigurations.JobApplyConfiguration, opts metav1.ApplyOptions) (result *v1.Job, err error) { +func (j jClient) ApplyStatus( + ctx context.Context, + job *batchv1applyconfigurations.JobApplyConfiguration, + opts metav1.ApplyOptions, +) (result *v1.Job, err error) { return nil, fmt.Errorf("not implemented") } diff --git a/mocks/pod.go b/mocks/pod.go index 02523c7..a8a73c9 100644 --- a/mocks/pod.go +++ b/mocks/pod.go @@ -17,8 +17,7 @@ import ( const MockContainerName = "TEST_CONTAINER" -type pClient struct { -} +type pClient struct{} const ( PodNotPresent = "NOT_PRESENT" @@ -32,15 +31,27 @@ const ( NoPodsMatchLabel = "NO_PODS" ) -func (p pClient) Create(ctx context.Context, pod *v1.Pod, opts metav1.CreateOptions) (*v1.Pod, error) { +func (p pClient) Create( + ctx context.Context, + pod *v1.Pod, + opts metav1.CreateOptions, +) (*v1.Pod, error) { return nil, fmt.Errorf("not implemented") } -func (p pClient) Update(ctx context.Context, pod *v1.Pod, opts metav1.UpdateOptions) (*v1.Pod, error) { +func (p pClient) Update( + ctx context.Context, + pod *v1.Pod, + opts metav1.UpdateOptions, +) (*v1.Pod, error) { return nil, fmt.Errorf("not implemented") } -func (p pClient) UpdateStatus(ctx context.Context, pod *v1.Pod, opts metav1.UpdateOptions) (*v1.Pod, error) { +func (p pClient) UpdateStatus( + ctx context.Context, + pod *v1.Pod, + opts metav1.UpdateOptions, +) (*v1.Pod, error) { return nil, fmt.Errorf("not implemented") } @@ -48,7 +59,11 @@ func (p pClient) Delete(ctx context.Context, name string, opts metav1.DeleteOpti return fmt.Errorf("not implemented") } -func (p pClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error { +func (p pClient) DeleteCollection( + ctx context.Context, + opts metav1.DeleteOptions, + listOpts metav1.ListOptions, +) error { return fmt.Errorf("not implemented") } @@ -111,23 +126,47 @@ func (p pClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Inte return nil, fmt.Errorf("not implemented") } -func (p pClient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *v1.Pod, err error) { +func (p pClient) Patch( + ctx context.Context, + name string, + pt types.PatchType, + data []byte, + opts metav1.PatchOptions, + subresources ...string, +) (result *v1.Pod, err error) { return nil, fmt.Errorf("not implemented") } -func (p pClient) Apply(ctx context.Context, pod *corev1applyconfigurations.PodApplyConfiguration, opts metav1.ApplyOptions) (result *v1.Pod, err error) { +func (p pClient) Apply( + ctx context.Context, + pod *corev1applyconfigurations.PodApplyConfiguration, + opts metav1.ApplyOptions, +) (result *v1.Pod, err error) { return nil, fmt.Errorf("not implemented") } -func (p pClient) ApplyStatus(ctx context.Context, pod *corev1applyconfigurations.PodApplyConfiguration, opts metav1.ApplyOptions) (result *v1.Pod, err error) { +func (p pClient) ApplyStatus( + ctx context.Context, + pod *corev1applyconfigurations.PodApplyConfiguration, + opts metav1.ApplyOptions, +) (result *v1.Pod, err error) { return nil, fmt.Errorf("not implemented") } -func (p pClient) UpdateEphemeralContainers(ctx context.Context, podName string, pod *v1.Pod, opts metav1.UpdateOptions) (*v1.Pod, error) { +func (p pClient) UpdateEphemeralContainers( + ctx context.Context, + podName string, + pod *v1.Pod, + opts metav1.UpdateOptions, +) (*v1.Pod, error) { return nil, fmt.Errorf("not implemented") } -func (p pClient) Bind(ctx context.Context, binding *v1.Binding, opts metav1.CreateOptions) error { +func (p pClient) Bind( + ctx context.Context, + binding *v1.Binding, + opts metav1.CreateOptions, +) error { return fmt.Errorf("not implemented") } @@ -147,7 +186,10 @@ func (p pClient) GetLogs(name string, opts *v1.PodLogOptions) *restclient.Reques return nil } -func (p pClient) ProxyGet(scheme, name, port, path string, params map[string]string) restclient.ResponseWrapper { +func (p pClient) ProxyGet( + scheme, name, port, path string, + params map[string]string, +) restclient.ResponseWrapper { return nil } diff --git a/mocks/service.go b/mocks/service.go index 53ab7a6..cbd77e3 100644 --- a/mocks/service.go +++ b/mocks/service.go @@ -13,8 +13,7 @@ import ( restclient "k8s.io/client-go/rest" ) -type sClient struct { -} +type sClient struct{} const ( MockServiceError = "mock service didnt work" @@ -23,15 +22,27 @@ const ( FailingServiceName = "fail" ) -func (s sClient) Create(ctx context.Context, service *v1.Service, opts metav1.CreateOptions) (*v1.Service, error) { +func (s sClient) Create( + ctx context.Context, + service *v1.Service, + opts metav1.CreateOptions, +) (*v1.Service, error) { return nil, fmt.Errorf("not implemented") } -func (s sClient) Update(ctx context.Context, service *v1.Service, opts metav1.UpdateOptions) (*v1.Service, error) { +func (s sClient) Update( + ctx context.Context, + service *v1.Service, + opts metav1.UpdateOptions, +) (*v1.Service, error) { return nil, fmt.Errorf("not implemented") } -func (s sClient) UpdateStatus(ctx context.Context, service *v1.Service, opts metav1.UpdateOptions) (*v1.Service, error) { +func (s sClient) UpdateStatus( + ctx context.Context, + service *v1.Service, + opts metav1.UpdateOptions, +) (*v1.Service, error) { return nil, fmt.Errorf("not implemented") } @@ -39,7 +50,11 @@ func (s sClient) Delete(ctx context.Context, name string, opts metav1.DeleteOpti return fmt.Errorf("not implemented") } -func (s sClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Service, error) { +func (s sClient) Get( + ctx context.Context, + name string, + opts metav1.GetOptions, +) (*v1.Service, error) { if name == FailingServiceName { return nil, fmt.Errorf(MockServiceError) } @@ -56,19 +71,37 @@ func (s sClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Inte return nil, fmt.Errorf("not implemented") } -func (s sClient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *v1.Service, err error) { +func (s sClient) Patch( + ctx context.Context, + name string, + pt types.PatchType, + data []byte, + opts metav1.PatchOptions, + subresources ...string, +) (result *v1.Service, err error) { return nil, fmt.Errorf("not implemented") } -func (s sClient) Apply(ctx context.Context, service *corev1applyconfigurations.ServiceApplyConfiguration, opts metav1.ApplyOptions) (result *v1.Service, err error) { +func (s sClient) Apply( + ctx context.Context, + service *corev1applyconfigurations.ServiceApplyConfiguration, + opts metav1.ApplyOptions, +) (result *v1.Service, err error) { return nil, fmt.Errorf("not implemented") } -func (s sClient) ApplyStatus(ctx context.Context, service *corev1applyconfigurations.ServiceApplyConfiguration, opts metav1.ApplyOptions) (result *v1.Service, err error) { +func (s sClient) ApplyStatus( + ctx context.Context, + service *corev1applyconfigurations.ServiceApplyConfiguration, + opts metav1.ApplyOptions, +) (result *v1.Service, err error) { return nil, fmt.Errorf("not implemented") } -func (s sClient) ProxyGet(scheme, name, port, path string, params map[string]string) restclient.ResponseWrapper { +func (s sClient) ProxyGet( + scheme, name, port, path string, + params map[string]string, +) restclient.ResponseWrapper { return nil } diff --git a/playbooks/airship-kubernetes-entrypoint-lint.yaml b/playbooks/airship-kubernetes-entrypoint-lint.yaml new file mode 100644 index 0000000..29876b5 --- /dev/null +++ b/playbooks/airship-kubernetes-entrypoint-lint.yaml @@ -0,0 +1,20 @@ +# 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. + +- hosts: primary + tasks: + - name: Run Linter + block: + - name: "make docker-image-lint" + make: + chdir: "{{ zuul.project.src_dir }}" + target: docker-image-lint \ No newline at end of file diff --git a/playbooks/airship-kubernetes-entrypoint-lint-unit.yaml b/playbooks/airship-kubernetes-entrypoint-unit.yaml similarity index 76% rename from playbooks/airship-kubernetes-entrypoint-lint-unit.yaml rename to playbooks/airship-kubernetes-entrypoint-unit.yaml index cf735a6..b9cde8c 100644 --- a/playbooks/airship-kubernetes-entrypoint-lint-unit.yaml +++ b/playbooks/airship-kubernetes-entrypoint-unit.yaml @@ -12,16 +12,9 @@ - hosts: primary tasks: - - name: Run Linter - block: - - name: "make docker-image-lint" - make: - chdir: "{{ zuul.project.src_dir }}" - target: docker-image-lint - - name: Run Unit Tests block: - name: "make docker-image-unit-tests" make: chdir: "{{ zuul.project.src_dir }}" - target: docker-image-unit-tests + target: docker-image-unit-tests \ No newline at end of file diff --git a/tools/tools.go b/tools/tools.go index fd36b6e..c3a285c 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -1,3 +1,4 @@ +//go:build tools // +build tools package tools diff --git a/util/util.go b/util/util.go index 7ac00b6..abf7436 100644 --- a/util/util.go +++ b/util/util.go @@ -24,7 +24,7 @@ func GetIp() (string, error) { if err != nil || len(address) == 0 { return "", fmt.Errorf("cannot get ip: %v", err) } - //Take first element to get rid of subnet + // Take first element to get rid of subnet ip := strings.Split(address[0].String(), "/")[0] return ip, nil } diff --git a/util/util_test.go b/util/util_test.go index c42b996..b955bc7 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -8,7 +8,6 @@ import ( const failingNamespaceUtil = "foo:util" var _ = Describe("Util", func() { - It("fails on trying to resolve a socket with namespace", func() { contains := ContainsSeparator(failingNamespaceUtil, "Util") Expect(contains).To(Equal(true)) diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml index 3bd05fc..8ea5856 100644 --- a/zuul.d/jobs.yaml +++ b/zuul.d/jobs.yaml @@ -16,6 +16,21 @@ run: playbooks/airship-kubernetes-entrypoint-lint-unit.yaml nodeset: airship-kubernetes-entrypoint-single-node-focal + +- job: + name: airship-kubernetes-entrypoint-lint + timeout: 3600 + pre-run: playbooks/airship-kubernetes-entrypoint-deploy-docker.yaml + run: playbooks/airship-kubernetes-entrypoint-lint.yaml + nodeset: airship-kubernetes-entrypoint-single-node-focal + +- job: + name: airship-kubernetes-entrypoint-unit + timeout: 3600 + pre-run: playbooks/airship-kubernetes-entrypoint-deploy-docker.yaml + run: playbooks/airship-kubernetes-entrypoint-unit.yaml + nodeset: airship-kubernetes-entrypoint-single-node-focal + - job: name: airship-kubernetes-entrypoint-docker-build-gate-ubuntu_focal timeout: 3600 diff --git a/zuul.d/projects.yaml b/zuul.d/projects.yaml index edde05c..14fcc82 100644 --- a/zuul.d/projects.yaml +++ b/zuul.d/projects.yaml @@ -13,11 +13,14 @@ - project: check: jobs: - - airship-kubernetes-entrypoint-lint-unit + - airship-kubernetes-entrypoint-lint + - airship-kubernetes-entrypoint-unit - airship-kubernetes-entrypoint-docker-build-gate-ubuntu_focal + gate: jobs: - - airship-kubernetes-entrypoint-lint-unit + - airship-kubernetes-entrypoint-lint + - airship-kubernetes-entrypoint-unit - airship-kubernetes-entrypoint-docker-build-gate-ubuntu_focal post: jobs: