Skip to content

Commit

Permalink
Replace test envs with t.Setenv
Browse files Browse the repository at this point in the history
No description

---

Pull Request resolved: #181
commit_hash:713d3f9c49dfa52a3b5c1cc81fe81b6deb91747d
  • Loading branch information
kamushadenes authored and robot-piglet committed Jan 27, 2025
1 parent 68852ea commit b504cf4
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 60 deletions.
19 changes: 6 additions & 13 deletions cmd/trcli/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package config

import (
"os"
"testing"

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

func TestParseTransferYaml_WithEnvSubstitution(t *testing.T) {
require.NoError(t, os.Setenv("FOO", "secret1"))
require.NoError(t, os.Setenv("BAR", "secret2"))
defer os.Unsetenv("FOO")
defer os.Unsetenv("BAR")
t.Setenv("FOO", "secret1")
t.Setenv("BAR", "secret2")

transfer, err := ParseTransferYaml([]byte(`
src:
Expand All @@ -31,10 +28,8 @@ dst:
}

func TestParserTransferYaml_WithRawYaml(t *testing.T) {
require.NoError(t, os.Setenv("FOO", "secret1"))
require.NoError(t, os.Setenv("BAR", "secret2"))
defer os.Unsetenv("FOO")
defer os.Unsetenv("BAR")
t.Setenv("FOO", "secret1")
t.Setenv("BAR", "secret2")

transfer, err := ParseTransferYaml([]byte(`
src:
Expand All @@ -53,10 +48,8 @@ dst:
}

func TestParserTransferYaml_WithYaml(t *testing.T) {
require.NoError(t, os.Setenv("FOO", "secret1"))
require.NoError(t, os.Setenv("BAR", "secret2"))
defer os.Unsetenv("FOO")
defer os.Unsetenv("BAR")
t.Setenv("FOO", "secret1")
t.Setenv("BAR", "secret2")

transfer, err := ParseTransferYaml([]byte(`
src:
Expand Down
34 changes: 19 additions & 15 deletions pkg/abstract/model/endpoint_rotator_config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model

import (
"os"
"testing"
"time"

Expand All @@ -26,12 +25,20 @@ func TestRotatorConfig(t *testing.T) {
}

// some month utility
var monthIds = map[time.Month]int{time.January: 0, time.February: 1, time.March: 2, time.April: 3, time.May: 4,
time.June: 5, time.July: 6, time.August: 7, time.September: 8, time.October: 9, time.November: 10, time.December: 11}
var monthList = []time.Month{time.January, time.February, time.March, time.April, time.May,
time.June, time.July, time.August, time.September, time.October, time.November, time.December}
var monthDayCount = []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
var monthDayCountLeap = []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
var monthIds = map[time.Month]int{
time.January: 0, time.February: 1, time.March: 2, time.April: 3, time.May: 4,
time.June: 5, time.July: 6, time.August: 7, time.September: 8, time.October: 9, time.November: 10, time.December: 11,
}

var monthList = []time.Month{
time.January, time.February, time.March, time.April, time.May,
time.June, time.July, time.August, time.September, time.October, time.November, time.December,
}

var (
monthDayCount = []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
monthDayCountLeap = []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
)

// scenario tests
func scenarioTesting(t *testing.T) {
Expand Down Expand Up @@ -83,7 +90,6 @@ func scenarioTestingDTSUPPORT693(t *testing.T) {
timestamp = timestamp.Add(time.Hour)
require.Equal(t, rotator.KeepPartCount, len(rotationTables), "Check that there is always window of six tables")
}

}

// tests
Expand Down Expand Up @@ -120,7 +126,6 @@ func validate(t *testing.T) {
require.Error(t, (&RotatorConfig{KeepPartCount: 1, PartType: RotatorPartHour, PartSize: -1, TimeColumn: ""}).Validate(), "PartSize should be positive")
require.Error(t, (&RotatorConfig{KeepPartCount: 0, PartType: RotatorPartHour, PartSize: 1, TimeColumn: ""}).Validate(), "KeepPartCount should be positive")
require.Error(t, (&RotatorConfig{KeepPartCount: 1, PartType: "kek", PartSize: 1, TimeColumn: ""}).Validate(), "wrong enum value of PartType")

}

func getMonthPartitionedTestLight(t *testing.T) {
Expand Down Expand Up @@ -174,14 +179,13 @@ func getMonthPartitionedTestHeavy(t *testing.T) {
require.Equal(t, monthList[i-(i%partSize)], rc.getMonthPartitioned(month))
}
}

}

func offsetDateTest(t *testing.T) {
t.Parallel()
t.Setenv("TZ", "Europe/Moscow") // this test is timezone aware
t.Run("Hours", offsetDateTestHours)
t.Run("Days", offsetDateTestDays)
//t.Run("MonthHeavy", offsetDateTestMonthHeavy) // TODO(@kry127) temporary switched off
// t.Run("MonthHeavy", offsetDateTestMonthHeavy) // TODO(@kry127) temporary switched off
t.Run("NilReceiver", offsetDateTestNilReceiver)
}

Expand All @@ -206,8 +210,6 @@ func offsetDateTestHours(t *testing.T) {

func offsetDateTestDays(t *testing.T) {
t.Parallel()
_ = os.Setenv("TZ", "Europe/Moscow") // this test is timezone aware
defer os.Unsetenv("TZ")
rcDays := RotatorConfig{KeepPartCount: 0, PartType: RotatorPartDay, PartSize: 1, TimeColumn: ""}
rcDaysTimestamp := time.Now()

Expand Down Expand Up @@ -281,6 +283,7 @@ func countDaysForYearAcc(year, month, offset int, acc int64) int64 {
return acc
}
}

func countDaysForYear(year, month, offset int) int64 {
return countDaysForYearAcc(year, month, offset, 0)
}
Expand Down Expand Up @@ -327,7 +330,6 @@ func offsetDateTestMonthHeavy(t *testing.T) {
t.Run("check year 2100 part size 1", func(t *testing.T) { checkYear(t, 2100, 1) })
t.Run("check year 2100 part size 5", func(t *testing.T) { checkYear(t, 2100, 5) })
t.Run("check year 2100 part size 12", func(t *testing.T) { checkYear(t, 2100, 12) })

}

func getPartitionBin(t *testing.T) {
Expand Down Expand Up @@ -358,12 +360,14 @@ func annotateWithTimeFromColumnTest(t *testing.T) {
t.Run("NoTimeColumnInConfig", annotateWithTimeFromColumnTestNoTimeColumnInConfig)
t.Run("NilReceiver", annotateWithTimeFromColumnTestNilReceiver)
}

func annotateWithTimeFromColumnTestWithoutFormat(t *testing.T) {
t.Parallel()
t.Run("Hours", annotateWithTimeFromColumnTestWithoutFormatHours)
t.Run("Days", annotateWithTimeFromColumnTestWithoutFormatDays)
t.Run("Months", annotateWithTimeFromColumnTestWithoutFormatMonths)
}

func annotateWithTimeFromColumnTestWithFormat(t *testing.T) {
t.Parallel()
t.Run("Hours", annotateWithTimeFromColumnTestWithTemplateHours)
Expand Down
4 changes: 1 addition & 3 deletions pkg/abstract/typed_change_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package abstract
import (
"encoding/json"
"fmt"
"os"
"testing"
"time"

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

func TestTypedChangeItem(t *testing.T) {
_ = os.Setenv("TZ", "NZ") // see: https://github.com/golang/go/issues/45960
defer os.Unsetenv("TZ")
t.Setenv("TZ", "NZ") // see: https://github.com/golang/go/issues/45960
ci := &TypedChangeItem{
ID: 291975574,
CommitTime: 1601382119000000000,
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/clickhouse/httpclient/http_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/providers/kafka/writer/writer_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/providers/yt/recipe/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewEnv(t *testing.T, opts ...yttest.Option) (*yttest.Env, func()) {
}
proxy, err := container.ConnectionHost(ctx)
require.NoError(t, err)
require.NoError(t, os.Setenv("YT_PROXY", proxy))
t.Setenv("YT_PROXY", proxy)
ytClient, err := container.NewClient(ctx)
require.NoError(t, err)
logger, stopLogger := yttest.NewLogger(t)
Expand Down
3 changes: 1 addition & 2 deletions tests/canon/clickhouse/canon_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package clickhouse

import (
"os"
"sort"
"strings"
"testing"
Expand Down Expand Up @@ -50,7 +49,7 @@ func getBaseType(colSchema abstract.ColSchema) string {
}

func TestCanonSource(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga
Source := &model.ChSource{
ShardsList: []model.ClickHouseShard{
{
Expand Down
2 changes: 1 addition & 1 deletion tests/canon/mongo/canon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func TestCanonSource(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga
databaseName := "canondb"
t.Run("vanilla hetero case", func(t *testing.T) {
snapshotPlusIncrementScenario(t, databaseName, "hetero_repack", false, false)
Expand Down
5 changes: 2 additions & 3 deletions tests/canon/mysql/canon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func execBatch(t *testing.T, conn *sql.DB, sqlCommands string) {
}

func TestCanonSource(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga
Source := &mysql.MysqlSource{
ClusterID: os.Getenv("CLUSTER_ID"),
Host: os.Getenv("RECIPE_MYSQL_HOST"),
Expand All @@ -47,7 +47,6 @@ func TestCanonSource(t *testing.T) {
}()

tableCase := func(tableName string) func(t *testing.T) {

return func(t *testing.T) {
connParams, err := mysql.NewConnectionParams(Source.ToStorageParams())
require.NoError(t, err)
Expand Down Expand Up @@ -106,5 +105,5 @@ func TestCanonSource(t *testing.T) {
t.Run("numeric_types_int", tableCase("numeric_types_int"))
t.Run("string_types", tableCase("string_types"))
t.Run("string_types_emoji", tableCase("string_types_emoji"))
//t.Run("spatial_types", tableCase("spatial_types"))
// t.Run("spatial_types", tableCase("spatial_types"))
}
2 changes: 1 addition & 1 deletion tests/canon/postgres/canon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestCanonSource(t *testing.T) {
if tcrecipes.Enabled() {
_ = pgrecipe.RecipeSource(pgrecipe.WithPrefix(""), pgrecipe.WithInitDir("dump"))
}
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga
srcPort := helpers.GetIntFromEnv("PG_LOCAL_PORT")
Source := &postgres.PgSource{
ClusterID: os.Getenv("PG_CLUSTER_ID"),
Expand Down
6 changes: 3 additions & 3 deletions tests/canon/s3/csv/canon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

func TestCanonSource(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga

testCasePath := "test_csv_all_types"
src := s3.PrepareCfg(t, "", "")
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestCanonSource(t *testing.T) {
var processed []abstract.ChangeItem

func TestNativeS3WithProvidedSchemaAndSystemCols(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga

processed = make([]abstract.ChangeItem, 0)
testCasePath := "test_csv_all_types"
Expand Down Expand Up @@ -258,7 +258,7 @@ func storeItems(item []abstract.ChangeItem) []abstract.ChangeItem {
}

func TestNativeS3MissingColumnsAreFilled(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga

processed = make([]abstract.ChangeItem, 0)
testCasePath := "test_csv_all_types"
Expand Down
2 changes: 1 addition & 1 deletion tests/canon/s3/jsonline/canon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestCanonSource(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga

testCasePath := "test_jsonline_all_types"
src := s3.PrepareCfg(t, "", "")
Expand Down
4 changes: 2 additions & 2 deletions tests/canon/s3/parquet/canon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func TestUnsopportedData(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga
absPath, err := filepath.Abs("unsupported_data")
require.NoError(t, err)
files, err := os.ReadDir(absPath)
Expand Down Expand Up @@ -82,7 +82,7 @@ func (r *rowsCutter) Push(items []abstract.ChangeItem) error {
}

func TestCanonSource(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga
absPath, err := filepath.Abs("data")
require.NoError(t, err)
files, err := os.ReadDir(absPath)
Expand Down
2 changes: 1 addition & 1 deletion tests/canon/sequences/sequences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
)

func TestCanonizeSequences(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga
Source := &pgcommon.PgSource{
ClusterID: os.Getenv("PG_CLUSTER_ID"),
Hosts: []string{"localhost"},
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/mysql2yt/date_time/check_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestDateTime(t *testing.T) {
))
}()

_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga

ctx := context.Background()

Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/pg2ch/dbt/check_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (

func TestSnapshot(t *testing.T) {
t.Skip()
_ = os.Setenv("DBT_CONTAINER_REGISTRY", "12197361.preprod")
_ = os.Setenv("DBT_IMAGE_TAG", "public.ecr.aws/t9p9v8b9")
t.Setenv("DBT_CONTAINER_REGISTRY", "12197361.preprod")
t.Setenv("DBT_IMAGE_TAG", "public.ecr.aws/t9p9v8b9")

source := pgrecipe.RecipeSource(
pgrecipe.WithInitFiles(yatest.SourcePath("transfer_manager/go/tests/e2e/pg2ch/dbt/init_pg.sql")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package replication
import (
"context"
"fmt"
"os"
"testing"
"time"

Expand All @@ -25,11 +24,11 @@ var (
)

func TestSnapshotAndIncrement(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga

Source := pgrecipe.RecipeSource(pgrecipe.WithPrefix(""))
Target := chrecipe.MustTarget(chrecipe.WithInitDir("dump/ch"), chrecipe.WithDatabase(databaseName))
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga
helpers.InitSrcDst(helpers.TransferID, Source, Target, TransferType) // to WithDefaults() & FillDependentFields(): IsHomo, helpers.TransferID, IsUpdateable
defer func() {
require.NoError(t, helpers.CheckConnections(
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/pg2mock/retry_conn_leak/check_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"os"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -35,7 +34,7 @@ func (s *mockSinker) Push(input []abstract.ChangeItem) error {
}

func TestReplication(t *testing.T) {
_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga
sinker := &mockSinker{}
transfer := model.Transfer{
ID: "test_id",
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/pg2ydb/replication_toasted/check_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestSnapshotAndIncrement(t *testing.T) {
Instance: helpers.GetEnvOfFail(t, "YDB_ENDPOINT"),
}

_ = os.Setenv("YC", "1") // to not go to vanga
t.Setenv("YC", "1") // to not go to vanga
helpers.InitSrcDst(helpers.TransferID, Source, Target, TransferType) // to WithDefaults() & FillDependentFields(): IsHomo, helpers.TransferID, IsUpdateable

defer func() {
Expand Down
Loading

0 comments on commit b504cf4

Please sign in to comment.