Skip to content

Commit

Permalink
Enable tests
Browse files Browse the repository at this point in the history
Enable tests, add test scaffolding for IPPool resource, speed up
compilation

Signed-Off-By: Moritz Roehrich <[email protected]>
  • Loading branch information
m-ildefons committed Aug 14, 2024
1 parent c03398d commit 941b5e1
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Dockerfile.dapper
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ ENV DAPPER_OUTPUT ./bin ./dist
ENV DAPPER_DOCKER_SOCKET true
ENV HOME ${DAPPER_SOURCE}

COPY go.mod ${DAPPER_SOURCE}/go.mod
COPY go.sum ${DAPPER_SOURCE}/go.sum
WORKDIR ${DAPPER_SOURCE}
RUN go mod download

ENTRYPOINT ["./scripts/entry"]
CMD ["ci"]
17 changes: 17 additions & 0 deletions internal/provider/ippool/resource_ippool_constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,26 @@ func (c *Constructor) subresourceIPPoolSelectorParser(data interface{}) error {
priority := uint32(ippoolSelector[constants.FieldSelectorPriority].(int))
network := ippoolSelector[constants.FieldSelectorNetwork].(string)

scopesData := ippoolSelector[constants.SubresourceTypeIPPoolSelectorScope].([]interface{})

scopes := []loadbalancerv1.Tuple{}
for _, scopeData := range scopesData {
scope := scopeData.(map[string]interface{})
scopeProject := scope[constants.FieldScopeProject].(string)
scopeNamespace := scope[constants.FieldScopeNamespace].(string)
scopeGuestCluster := scope[constants.FieldScopeGuestCluster].(string)

scopes = append(scopes, loadbalancerv1.Tuple{
Project: scopeProject,
Namespace: scopeNamespace,
GuestCluster: scopeGuestCluster,
})
}

c.IPPool.Spec.Selector = loadbalancerv1.Selector{
Priority: priority,
Network: network,
Scope: scopes,
}
return nil
}
2 changes: 1 addition & 1 deletion internal/provider/ippool/schema_ippool.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func subresourceSchemaIPPoolSelector() map[string]*schema.Schema {
Optional: true,
Description: "Namespace/name of the VM network",
},
constants.FieldSelectorScope: {
constants.SubresourceTypeIPPoolSelectorScope: {
Type: schema.TypeList,
Optional: true,
Description: "Scope of the IP pool",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package loadbalancer

import (
"fmt"

loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1"
corev1 "k8s.io/api/core/v1"

"github.com/harvester/terraform-provider-harvester/internal/util"
"github.com/harvester/terraform-provider-harvester/pkg/constants"
Expand All @@ -16,10 +19,35 @@ type Constructor struct {
}

func (c *Constructor) Setup() util.Processors {
return util.NewProcessors().
processors := util.NewProcessors().
Tags(&c.LoadBalancer.Labels).
Description(&c.LoadBalancer.Annotations).
String(constants.FieldLoadBalancerDescription, &c.LoadBalancer.Spec.Description, false)

subresourceProcessors := []util.Processor{
{
Field: constants.FieldLoadBalancerWorkloadType,
Parser: c.subresourceLoadBalancerWorkloadTypeParser,
Required: false,
},
{
Field: constants.FieldLoadBalancerIPAM,
Parser: c.subresourceLoadBalancerIPAMParser,
Required: false,
},
{
Field: constants.SubresourceTypeLoadBalancerListener,
Parser: c.subresourceLoadBalancerListenerParser,
Required: true,
},
{
Field: constants.SubresourceTypeLoadBalancerHealthCheck,
Parser: c.subresourceLoadBalancerHealthCheckParser,
Required: false,
},
}

return append(processors, subresourceProcessors...)
}

func (c *Constructor) Validate() error {
Expand All @@ -46,3 +74,48 @@ func Creator(namespace, name string) util.Constructor {
func Updater(loadbalancer *loadbalancerv1.LoadBalancer) util.Constructor {
return newLoadBalancerConstructor(loadbalancer)
}

func (c *Constructor) subresourceLoadBalancerWorkloadTypeParser(data interface{}) error {
workloadType := data.(string)

if workloadType != "vm" && workloadType != "cluster" {
return fmt.Errorf("invalid value for workload type: %v", workloadType)
}

c.LoadBalancer.Spec.WorkloadType = loadbalancerv1.WorkloadType(workloadType)

return nil
}

func (c *Constructor) subresourceLoadBalancerIPAMParser(data interface{}) error {
ipam := data.(string)

if ipam != "dhcp" && ipam != "cluster" {
return fmt.Errorf("invalid value for IPAM: %v", ipam)
}

c.LoadBalancer.Spec.IPAM = loadbalancerv1.IPAM(ipam)
return nil
}

func (c *Constructor) subresourceLoadBalancerListenerParser(data interface{}) error {
listener := data.(map[string]interface{})

name := listener[constants.FieldListenerName].(string)
port := int32(listener[constants.FieldListenerPort].(int))
protocol := corev1.Protocol(listener[constants.FieldListenerProtocol].(string))
backendPort := int32(listener[constants.FieldListenerBackendPort].(int))

c.LoadBalancer.Spec.Listeners = append(c.LoadBalancer.Spec.Listeners, loadbalancerv1.Listener{
Name: name,
Port: port,
Protocol: protocol,
BackendPort: backendPort,
})

return nil
}

func (c *Constructor) subresourceLoadBalancerHealthCheckParser(data interface{}) error {
return nil
}
40 changes: 40 additions & 0 deletions internal/tests/resource_ippool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tests

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

// loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1"
)

func TestIPPoolBasic(t *testing.T) {
// var (
// ippool *loadbalancerv1.IPPool
// ctx = context.Background()
// )

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: `
resource harvester_ippool \"test_ippool\" {
name = \"test_ippool\"
range {
range_start = \"192.168.0.1\"
range_end = \"192.168.0.254\"
range_subnet = \"192.168.0.1/24\"
range_gateway = \"192.168.0.1\"
}
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("harvester_ippool.test_ippool", "name", "test_ippool"),
),
},
},
})
}
1 change: 0 additions & 1 deletion pkg/constants/constants_ippool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (

FieldSelectorPriority = "priority"
FieldSelectorNetwork = "network"
FieldSelectorScope = "scope"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ cd $(dirname $0)/..

mkdir -p bin
[ "$(uname)" != "Darwin" ] && LINKFLAGS="-extldflags -static -s"
CGO_ENABLED=0 GOARCH=amd64 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/terraform-provider-harvester-amd64
CGO_ENABLED=0 GOARCH=arm64 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/terraform-provider-harvester-arm64
CGO_ENABLED=0 GOARCH=amd64 go build -cover -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/terraform-provider-harvester-amd64
# CGO_ENABLED=0 GOARCH=arm64 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/terraform-provider-harvester-arm64
2 changes: 1 addition & 1 deletion scripts/ci
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

cd $(dirname $0)

./validate
./build
./test
./validate
./package
2 changes: 1 addition & 1 deletion scripts/default
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

cd $(dirname $0)

./validate
./build
./test
./validate
./package
4 changes: 2 additions & 2 deletions scripts/test
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ set -e

cd $(dirname $0)/..

echo Running tests
go test -cover -tags=test .
echo Running tests:
go test -cover -tags=test . ./...
6 changes: 2 additions & 4 deletions scripts/validate
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ if ! command -v golangci-lint; then
exit
fi

echo Running validation

echo Running: golangci-lint
echo Running validation: golangci-lint
golangci-lint run --timeout 5m

echo Running: go fmt
echo Running validation: go fmt
test -z "$(go fmt ${PACKAGES} | tee /dev/stderr)"

0 comments on commit 941b5e1

Please sign in to comment.