Skip to content
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
2 changes: 2 additions & 0 deletions pkg/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ go_test(
"analysis_test.go",
"config_test.go",
"control_plane_test.go",
"deployment_cloudrun_test.go",
"deployment_kubernetes_test.go",
"deployment_lambda_test.go",
"deployment_terraform_test.go",
"deployment_test.go",
"event_watcher_test.go",
Expand Down
17 changes: 13 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,19 @@ func (c *Config) Validate() error {
if c.APIVersion != versionV1Beta1 {
return fmt.Errorf("unsupported version: %s", c.APIVersion)
}
if spec, ok := c.spec.(validator); ok && spec != nil {
if err := spec.Validate(); err != nil {
return err
}
if c.Kind == "" {
return fmt.Errorf("kind is required")
}
if c.spec == nil {
return fmt.Errorf("spec is required")
}

spec, ok := c.spec.(validator)
if !ok {
return fmt.Errorf("spec must have Validate function")
}
if err := spec.Validate(); err != nil {
return err
}
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/deployment_cloudrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type CloudRunDeploymentSpec struct {

// Validate returns an error if any wrong configuration value was found.
func (s *CloudRunDeploymentSpec) Validate() error {
if err := s.GenericDeploymentSpec.Validate(); err != nil {
return err
}
return nil
}

Expand Down
59 changes: 59 additions & 0 deletions pkg/config/deployment_cloudrun_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020 The PipeCD 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 config

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCloudRunDeploymentConfig(t *testing.T) {
testcases := []struct {
fileName string
expectedKind Kind
expectedAPIVersion string
expectedSpec interface{}
expectedError error
}{
{
fileName: "testdata/application/cloudrun-app.yaml",
expectedKind: KindCloudRunApp,
expectedAPIVersion: "pipecd.dev/v1beta1",
expectedSpec: &CloudRunDeploymentSpec{
GenericDeploymentSpec: GenericDeploymentSpec{
Timeout: Duration(6 * time.Hour),
},
Input: CloudRunDeploymentInput{
AutoRollback: true,
},
},
expectedError: nil,
},
}
for _, tc := range testcases {
t.Run(tc.fileName, func(t *testing.T) {
cfg, err := LoadFromYAML(tc.fileName)
require.Equal(t, tc.expectedError, err)
if err == nil {
assert.Equal(t, tc.expectedKind, cfg.Kind)
assert.Equal(t, tc.expectedAPIVersion, cfg.APIVersion)
assert.Equal(t, tc.expectedSpec, cfg.spec)
}
})
}
}
3 changes: 3 additions & 0 deletions pkg/config/deployment_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type LambdaDeploymentSpec struct {

// Validate returns an error if any wrong configuration value was found.
func (s *LambdaDeploymentSpec) Validate() error {
if err := s.GenericDeploymentSpec.Validate(); err != nil {
return err
}
return nil
}

Expand Down
59 changes: 59 additions & 0 deletions pkg/config/deployment_lambda_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020 The PipeCD 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 config

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLambdaDeploymentConfig(t *testing.T) {
testcases := []struct {
fileName string
expectedKind Kind
expectedAPIVersion string
expectedSpec interface{}
expectedError error
}{
{
fileName: "testdata/application/lambda-app.yaml",
expectedKind: KindLambdaApp,
expectedAPIVersion: "pipecd.dev/v1beta1",
expectedSpec: &LambdaDeploymentSpec{
GenericDeploymentSpec: GenericDeploymentSpec{
Timeout: Duration(6 * time.Hour),
},
Input: LambdaDeploymentInput{
AutoRollback: true,
},
},
expectedError: nil,
},
}
for _, tc := range testcases {
t.Run(tc.fileName, func(t *testing.T) {
cfg, err := LoadFromYAML(tc.fileName)
require.Equal(t, tc.expectedError, err)
if err == nil {
assert.Equal(t, tc.expectedKind, cfg.Kind)
assert.Equal(t, tc.expectedAPIVersion, cfg.APIVersion)
assert.Equal(t, tc.expectedSpec, cfg.spec)
}
})
}
}
3 changes: 3 additions & 0 deletions pkg/config/deployment_terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type TerraformDeploymentSpec struct {

// Validate returns an error if any wrong configuration value was found.
func (s *TerraformDeploymentSpec) Validate() error {
if err := s.GenericDeploymentSpec.Validate(); err != nil {
return err
}
return nil
}

Expand Down
10 changes: 9 additions & 1 deletion pkg/config/deployment_terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package config

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -36,6 +37,9 @@ func TestTerraformDeploymentConfig(t *testing.T) {
expectedKind: KindTerraformApp,
expectedAPIVersion: "pipecd.dev/v1beta1",
expectedSpec: &TerraformDeploymentSpec{
GenericDeploymentSpec: GenericDeploymentSpec{
Timeout: Duration(6 * time.Hour),
},
Input: TerraformDeploymentInput{},
},
expectedError: nil,
Expand All @@ -45,7 +49,9 @@ func TestTerraformDeploymentConfig(t *testing.T) {
expectedKind: KindTerraformApp,
expectedAPIVersion: "pipecd.dev/v1beta1",
expectedSpec: &TerraformDeploymentSpec{
GenericDeploymentSpec: GenericDeploymentSpec{},
GenericDeploymentSpec: GenericDeploymentSpec{
Timeout: Duration(6 * time.Hour),
},
Input: TerraformDeploymentInput{
Workspace: "dev",
TerraformVersion: "0.12.23",
Expand All @@ -66,6 +72,7 @@ func TestTerraformDeploymentConfig(t *testing.T) {
OutFilename: "service-account.yaml",
},
},
Timeout: Duration(6 * time.Hour),
},
Input: TerraformDeploymentInput{
Workspace: "dev",
Expand Down Expand Up @@ -100,6 +107,7 @@ func TestTerraformDeploymentConfig(t *testing.T) {
},
},
},
Timeout: Duration(6 * time.Hour),
},
Input: TerraformDeploymentInput{
Workspace: "dev",
Expand Down
4 changes: 0 additions & 4 deletions pkg/config/testdata/application/cloudrun-app.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
apiVersion: pipecd.dev/v1beta1
kind: CloudRunApp
spec:
input:
image: gcr.io/demo-project/demoapp:v1.0.0

7 changes: 0 additions & 7 deletions pkg/config/testdata/application/lambda-app.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
# https://docs.aws.amazon.com/lambda/latest/dg/configuration-versions.html
apiVersion: pipecd.dev/v1beta1
kind: LambdaApp
spec:
input:
# Where to fetch the source code to create lambda package.
git: [email protected]:org/source-repo.git
path: lambdas/demoapp
ref: v1.0.0