Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Add test for aws config check
Browse files Browse the repository at this point in the history
Signed-off-by: knrt10 <kautilya@kinvolk.io>
  • Loading branch information
knrt10 committed Sep 2, 2020
1 parent 3a81232 commit 69a85b4
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions pkg/platform/aws/aws_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2020 The Lokomotive 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 aws

import (
"testing"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclparse"

lokoconfig "github.com/kinvolk/lokomotive/pkg/config"
)

// loadConfigFromString loads config from string.
func loadConfigFromString(t *testing.T, c string) hcl.Diagnostics {
p := hclparse.NewParser()

f, d := p.ParseHCL([]byte(c), "x.lokocfg")
if d.HasErrors() {
t.Fatalf("parsing HCL should succeed, got: %v", d)
}

configBody := hcl.MergeFiles([]*hcl.File{f})

var rootConfig lokoconfig.RootConfig

if d := gohcl.DecodeBody(configBody, nil, &rootConfig); d.HasErrors() {
t.Fatalf("decoding root config should succeed, got: %v", d)
}

cc := &config{}

return cc.LoadConfig(&rootConfig.Cluster.Config, &hcl.EvalContext{})
}

func TestLoadConfig(t *testing.T) {
c := `
cluster "aws" {
asset_dir = "/fooo"
cluster_name = "mycluster"
dns_zone = "testzone"
dns_zone_id = "testzoneID"
ssh_pubkeys = ["testkey"]
worker_pool "foo" {
count = 1
ssh_pubkeys = ["testkey"]
}
}
`

if d := loadConfigFromString(t, c); d.HasErrors() {
t.Fatalf("valid config should not return error, got: %v", d)
}
}

func TestNoPortForMuliplePools(t *testing.T) {
c := config{
WorkerPools: []workerPool{
{
Name: "pool-1",
},
{
Name: "pool-2",
},
},
}

if d := c.checkLBPortsUnique(); !d.HasErrors() {
t.Error("Should fail with lb_http_port and lb_https_port not set")
}
}

func TestDefaultPortForMuliplePools(t *testing.T) {
c := config{
WorkerPools: []workerPool{
{
Name: "pool-1",
},
{
Name: "pool-2",
LBHTTPPort: 80,
LBHTTPSPort: 443,
},
},
}

if d := c.checkLBPortsUnique(); !d.HasErrors() {
t.Error("Should fail with default lb_http_port and lb_https_port")
}
}

func TestUniquePortForMuliplePools(t *testing.T) {
c := config{
WorkerPools: []workerPool{
{
Name: "pool-1",
},
{
Name: "pool-2",
LBHTTPPort: 8080,
LBHTTPSPort: 8443,
},
},
}

if d := c.checkLBPortsUnique(); d.HasErrors() {
t.Error("Should work with unique lb_http_port and lb_https_port")
}
}

0 comments on commit 69a85b4

Please sign in to comment.