Skip to content

[WIP] Fix CI tests by prioritizing environment variables for config#5

Closed
Copilot wants to merge 3 commits into
v3from
copilot/update-load-test-config-function
Closed

[WIP] Fix CI tests by prioritizing environment variables for config#5
Copilot wants to merge 3 commits into
v3from
copilot/update-load-test-config-function

Conversation

Copy link
Copy Markdown

Copilot AI commented Jan 19, 2026

Fix CI test failures in transfer package

  • Update transfer/transfer_test.go to add strings import
  • Replace loadTestConfig function to prioritize environment variables over config file
  • Add getEnvOrDefault helper function for default values
  • Update .github/workflows/testing.yml to add NAMESPACE environment variable
  • Verify changes work with environment variables
  • Translate Chinese comments to English for consistency
Original prompt

Problem

The CI tests are failing because transfer/transfer_test.go tries to read ../config/values.yml which doesn't exist in the CI environment:

failed to read config: open ../config/values.yml: no such file or directory

This affects all tests in the transfer package that use setupNatsConnection().

Solution

Modify the loadTestConfig function to prioritize environment variables (which are already set in the GitHub Actions workflow) over the config file. This allows:

  • ✅ CI to work with environment variables
  • ✅ Local development to continue using config/values.yml

Changes Required

1. Update transfer/transfer_test.go

Add strings import at line 3-13:

import (
	"context"
	"os"
	"strings"  // ADD THIS LINE
	"testing"
	"time"

	"github.com/bytedance/sonic"
	"github.com/nats-io/nats.go"
	"github.com/nats-io/nats.go/jetstream"
	"gopkg.in/yaml.v3"
)

Replace the loadTestConfig function (currently at lines 278-288):

func loadTestConfig(t *testing.T) *testConfig {
	// 优先使用环境变量(CI 环境)
	if natsHosts := os.Getenv("NATS_HOSTS"); natsHosts != "" {
		return &testConfig{
			Namespace: getEnvOrDefault("NAMESPACE", "auditstream"),
			NatsHosts: strings.Split(natsHosts, ","),
			NatsToken: os.Getenv("NATS_TOKEN"),
		}
	}
	
	// 本地开发使用配置文件
	data, err := os.ReadFile("../config/values.yml")
	if err != nil {
		t.Fatalf("failed to read config: %v", err)
	}
	var cfg testConfig
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		t.Fatalf("failed to parse config: %v", err)
	}
	return &cfg
}

Add helper function after loadTestConfig:

func getEnvOrDefault(key, defaultValue string) string {
	if value := os.Getenv(key); value != "" {
		return value
	}
	return defaultValue
}

2. Update .github/workflows/testing.yml

Add NAMESPACE environment variable to the env section (around line 21-25):

env:
  GO111MODULE: on
  NATS_HOSTS: nats://127.0.0.1:4222,nats://127.0.0.1:4223,nats://127.0.0.1:4224
  NATS_TOKEN: s3cr3t
  NAMESPACE: auditstream  # ADD THIS LINE
  AUTO: 1

Context

The GitHub Actions workflow already:

  • Starts a NATS cluster via docker-compose
  • Sets environment variables: NATS_HOSTS, NATS_TOKEN, AUTO
  • These match the NATS configuration in .github/compose/docker-compose.yml

By reading from environment variables first, the tests will work in CI while still supporting local development with config files.

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 19, 2026 07:52
Co-authored-by: kainonly <9986159+kainonly@users.noreply.github.com>
Co-authored-by: kainonly <9986159+kainonly@users.noreply.github.com>
@kainonly kainonly closed this Jan 19, 2026
Copilot AI requested a review from kainonly January 19, 2026 07:55
Copilot stopped work on behalf of kainonly due to an error January 19, 2026 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants