Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d11b7d2
Add testify and difflib dependencies to go.mod and go.sum
IsuruGunarathne Feb 5, 2026
6930756
Add unit tests for compilation functions in compiler_test.go
IsuruGunarathne Feb 5, 2026
5546e53
Add tests for module path extraction and directory structure validation
IsuruGunarathne Feb 5, 2026
eea27a9
Add unit tests for Docker command availability and execution
IsuruGunarathne Feb 5, 2026
6974d64
Add tests for handling invalid manifest YAML and writing to non-exist…
IsuruGunarathne Feb 5, 2026
33ee1a5
Add unit tests for Dockerfile generation and build instructions
IsuruGunarathne Feb 5, 2026
3c79dbc
Add unit tests for UpdateGoMod function handling remote policies
IsuruGunarathne Feb 5, 2026
9020909
Add tests for handling permission errors in path validation and file …
IsuruGunarathne Feb 5, 2026
1c99cc7
Add unit tests for DumpConfig and related functions in dumper_test.go
IsuruGunarathne Feb 5, 2026
a80b5f9
Add unit tests for NewServer and server lifecycle management in serve…
IsuruGunarathne Feb 5, 2026
0e48645
Add unit tests for policy execution context in execution_context_test.go
IsuruGunarathne Feb 5, 2026
058cddf
Add unit tests for external processor server in extproc_test.go
IsuruGunarathne Feb 5, 2026
90c8b8e
Add unit tests for header mutation and request translation in transla…
IsuruGunarathne Feb 5, 2026
3cb4a49
Add unit tests for metrics server functionality in server_test.go
IsuruGunarathne Feb 5, 2026
f7a3291
Add unit tests for access logger server and resource handler function…
IsuruGunarathne Feb 5, 2026
61e45c2
Refactor test files in gateway-builder
IsuruGunarathne Feb 5, 2026
e1baa81
Refactor test files in policy-engine
IsuruGunarathne Feb 5, 2026
1a3367b
Exclude testutils in test commands for gateway-builder and policy-engine
IsuruGunarathne Feb 5, 2026
a4f7b7b
Merge branch 'wso2:main' into improve-coverage-gateway-builder
IsuruGunarathne Feb 5, 2026
8c85447
Add unit tests for cmd/builder
IsuruGunarathne Feb 5, 2026
7b806a3
Improve test reliability by updating environment variable checks and …
IsuruGunarathne Feb 5, 2026
1682a31
Address coderabbit suggestions
IsuruGunarathne Feb 5, 2026
24cf65b
Fix formatting of policy version in build instructions and update tes…
IsuruGunarathne Feb 5, 2026
5ec8c44
Refactor timestamp validation in Dockerfile build test for improved c…
IsuruGunarathne Feb 5, 2026
c407a35
Enhance test coverage by improving error handling in Dockerfile tests…
IsuruGunarathne Feb 5, 2026
2dcfa4a
Add tests for output directory creation failure in generator functions
IsuruGunarathne Feb 5, 2026
daf3167
Merge branch 'wso2:main' into improve-coverage-gateway-builder
IsuruGunarathne Feb 5, 2026
90ff64a
Merge branch 'wso2:main' into improve-coverage-gateway-builder
IsuruGunarathne Feb 6, 2026
e2521f2
Merge branch 'wso2:main' into improve-coverage-gateway-builder
IsuruGunarathne Feb 6, 2026
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: 1 addition & 1 deletion gateway/gateway-builder/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ build-and-push-multiarch: ## Build and push multi-architecture Docker image (lin
.PHONY: test
test: ## Run tests
@echo "Running tests..."
@go test -v ./... -cover -coverprofile=unit-test-coverage.txt
@go test -v $$(go list ./... | grep -v /testutils) -cover -coverprofile=unit-test-coverage.txt

.PHONY: tidy
tidy: ## Run go mod tidy
Expand Down
160 changes: 160 additions & 0 deletions gateway/gateway-builder/cmd/builder/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you 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 main

import (
"bytes"
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/wso2/api-platform/gateway/gateway-builder/internal/docker"
"github.com/wso2/api-platform/gateway/gateway-builder/internal/manifest"
"github.com/wso2/api-platform/gateway/gateway-builder/pkg/types"
)

// TestInitLogger tests the logger initialization function
func TestInitLogger_JSONFormat(t *testing.T) {
// Capture that it doesn't panic with json format
assert.NotPanics(t, func() {
initLogger("json", "info")
})
}

func TestInitLogger_TextFormat(t *testing.T) {
// Capture that it doesn't panic with text format
assert.NotPanics(t, func() {
initLogger("text", "info")
})
}

func TestInitLogger_DebugLevel(t *testing.T) {
assert.NotPanics(t, func() {
initLogger("json", "debug")
})
}

func TestInitLogger_WarnLevel(t *testing.T) {
assert.NotPanics(t, func() {
initLogger("json", "warn")
})
}

func TestInitLogger_ErrorLevel(t *testing.T) {
assert.NotPanics(t, func() {
initLogger("json", "error")
})
}

func TestInitLogger_UnknownLevel(t *testing.T) {
// Unknown level should default to info without panicking
assert.NotPanics(t, func() {
initLogger("json", "unknown")
})
}

// TestPrintDockerfileGenerationSummary tests the summary printing function
func TestPrintDockerfileGenerationSummary(t *testing.T) {
// Create mock docker result
result := &docker.GenerateResult{
Success: true,
PolicyEngineDockerfile: "/tmp/output/Dockerfile.policy-engine",
GatewayControllerDockerfile: "/tmp/output/Dockerfile.gateway-controller",
RouterDockerfile: "/tmp/output/Dockerfile.router",
}

// Create mock policies
policies := []*types.DiscoveredPolicy{
{Name: "test-policy", Version: "v1.0.0"},
}

// Create mock manifest
buildManifest := manifest.CreateManifest("test-version", policies, "/tmp/output")

// Capture stdout
output := captureOutput(func() {
printDockerfileGenerationSummary(result, buildManifest, "/tmp/output/build-manifest.json")
})

// Verify expected content in output
assert.Contains(t, output, "Gateway Dockerfiles Generated")
assert.Contains(t, output, "Policy Engine")
assert.Contains(t, output, "Gateway Controller")
assert.Contains(t, output, "Router")
assert.Contains(t, output, "build-manifest.json")
}

func TestPrintDockerfileGenerationSummary_WithMultiplePolicies(t *testing.T) {
result := &docker.GenerateResult{
Success: true,
PolicyEngineDockerfile: "/output/Dockerfile.policy-engine",
GatewayControllerDockerfile: "/output/Dockerfile.gateway-controller",
RouterDockerfile: "/output/Dockerfile.router",
}

policies := []*types.DiscoveredPolicy{
{Name: "auth-policy", Version: "v1.0.0"},
{Name: "rate-limit", Version: "v2.0.0"},
{Name: "cors", Version: "v1.5.0"},
}

buildManifest := manifest.CreateManifest("1.0.0", policies, "/output")

output := captureOutput(func() {
printDockerfileGenerationSummary(result, buildManifest, "/output/manifest.json")
})

// Should contain policy count info in JSON manifest
assert.Contains(t, output, "auth-policy")
assert.Contains(t, output, "rate-limit")
assert.Contains(t, output, "cors")
}

// TestVersionVariables tests that version variables are set
func TestVersionVariables(t *testing.T) {
// These are set at compile time, but we can verify they have default values
assert.NotEmpty(t, Version, "Version should have a default value")
assert.NotEmpty(t, GitCommit, "GitCommit should have a default value")
assert.NotEmpty(t, BuildDate, "BuildDate should have a default value")
}

// TestDefaultConstants tests the default constant values
func TestDefaultConstants(t *testing.T) {
assert.Equal(t, "policy-manifest.yaml", DefaultManifestFile)
assert.Equal(t, "system-policy-manifest-lock.yaml", DefaultSystemPolicyManifestLockFile)
assert.Equal(t, "output", DefaultOutputDir)
assert.Equal(t, "/api-platform/gateway/policy-engine", DefaultPolicyEngineSrc)
}

// captureOutput is a helper to capture stdout during test execution
func captureOutput(f func()) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

f()

w.Close()
os.Stdout = old

var buf bytes.Buffer
buf.ReadFrom(r)
return buf.String()
}
Loading
Loading