Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -62,7 +61,7 @@ func TestBitbucketHasRepo(t *testing.T) {
}))
defer func() { testServer.Close() }()

os.Setenv("BITBUCKET_API_BASE_URL", testServer.URL)
t.Setenv("BITBUCKET_API_BASE_URL", testServer.URL)
cases := []struct {
name, path, repo, owner, sha string
status int
Expand Down Expand Up @@ -449,7 +448,7 @@ func TestBitbucketListRepos(t *testing.T) {
}))
defer func() { testServer.Close() }()

os.Setenv("BITBUCKET_API_BASE_URL", testServer.URL)
t.Setenv("BITBUCKET_API_BASE_URL", testServer.URL)
cases := []struct {
name, proto, owner string
hasError, allBranches bool
Expand Down
4 changes: 1 addition & 3 deletions controller/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -292,8 +291,7 @@ argocd_app_labels{label_non_existing="",name="my-app-3",namespace="argocd",proje
}

func TestLegacyMetrics(t *testing.T) {
os.Setenv(EnvVarLegacyControllerMetrics, "true")
defer os.Unsetenv(EnvVarLegacyControllerMetrics)
t.Setenv(EnvVarLegacyControllerMetrics, "true")

expectedResponse := `
# HELP argocd_app_created_time Creation time in unix timestamp for an application.
Expand Down
137 changes: 71 additions & 66 deletions controller/sharding/sharding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ import (
)

func TestGetShardByID_NotEmptyID(t *testing.T) {
os.Setenv(common.EnvControllerReplicas, "1")
t.Setenv(common.EnvControllerReplicas, "1")
assert.Equal(t, 0, LegacyDistributionFunction()(&v1alpha1.Cluster{ID: "1"}))
assert.Equal(t, 0, LegacyDistributionFunction()(&v1alpha1.Cluster{ID: "2"}))
assert.Equal(t, 0, LegacyDistributionFunction()(&v1alpha1.Cluster{ID: "3"}))
assert.Equal(t, 0, LegacyDistributionFunction()(&v1alpha1.Cluster{ID: "4"}))
}

func TestGetShardByID_EmptyID(t *testing.T) {
os.Setenv(common.EnvControllerReplicas, "1")
t.Setenv(common.EnvControllerReplicas, "1")
distributionFunction := LegacyDistributionFunction
shard := distributionFunction()(&v1alpha1.Cluster{})
assert.Equal(t, 0, shard)
}

func TestGetShardByID_NoReplicas(t *testing.T) {
os.Setenv(common.EnvControllerReplicas, "0")
t.Setenv(common.EnvControllerReplicas, "0")
distributionFunction := LegacyDistributionFunction
shard := distributionFunction()(&v1alpha1.Cluster{})
assert.Equal(t, -1, shard)
}

func TestGetShardByID_NoReplicasUsingHashDistributionFunction(t *testing.T) {
os.Setenv(common.EnvControllerReplicas, "0")
t.Setenv(common.EnvControllerReplicas, "0")
distributionFunction := LegacyDistributionFunction
shard := distributionFunction()(&v1alpha1.Cluster{})
assert.Equal(t, -1, shard)
Expand All @@ -45,22 +45,21 @@ func TestGetShardByID_NoReplicasUsingHashDistributionFunction(t *testing.T) {
func TestGetShardByID_NoReplicasUsingHashDistributionFunctionWithClusters(t *testing.T) {
db, cluster1, cluster2, cluster3, cluster4, cluster5 := createTestClusters()
// Test with replicas set to 0
os.Setenv(common.EnvControllerReplicas, "0")
os.Setenv(common.EnvControllerShardingAlgorithm, common.RoundRobinShardingAlgorithm)
t.Setenv(common.EnvControllerReplicas, "0")
t.Setenv(common.EnvControllerShardingAlgorithm, common.RoundRobinShardingAlgorithm)
distributionFunction := RoundRobinDistributionFunction(db)
assert.Equal(t, -1, distributionFunction(nil))
assert.Equal(t, -1, distributionFunction(&cluster1))
assert.Equal(t, -1, distributionFunction(&cluster2))
assert.Equal(t, -1, distributionFunction(&cluster3))
assert.Equal(t, -1, distributionFunction(&cluster4))
assert.Equal(t, -1, distributionFunction(&cluster5))

}

func TestGetClusterFilterDefault(t *testing.T) {
shardIndex := 1 // ensuring that a shard with index 1 will process all the clusters with an "even" id (2,4,6,...)
os.Unsetenv(common.EnvControllerShardingAlgorithm)
os.Setenv(common.EnvControllerReplicas, "2")
t.Setenv(common.EnvControllerReplicas, "2")
filter := GetClusterFilter(GetDistributionFunction(nil, common.DefaultShardingAlgorithm), shardIndex)
assert.False(t, filter(&v1alpha1.Cluster{ID: "1"}))
assert.True(t, filter(&v1alpha1.Cluster{ID: "2"}))
Expand All @@ -70,8 +69,8 @@ func TestGetClusterFilterDefault(t *testing.T) {

func TestGetClusterFilterLegacy(t *testing.T) {
shardIndex := 1 // ensuring that a shard with index 1 will process all the clusters with an "even" id (2,4,6,...)
os.Setenv(common.EnvControllerReplicas, "2")
os.Setenv(common.EnvControllerShardingAlgorithm, common.LegacyShardingAlgorithm)
t.Setenv(common.EnvControllerReplicas, "2")
t.Setenv(common.EnvControllerShardingAlgorithm, common.LegacyShardingAlgorithm)
filter := GetClusterFilter(GetDistributionFunction(nil, common.LegacyShardingAlgorithm), shardIndex)
assert.False(t, filter(&v1alpha1.Cluster{ID: "1"}))
assert.True(t, filter(&v1alpha1.Cluster{ID: "2"}))
Expand All @@ -81,8 +80,8 @@ func TestGetClusterFilterLegacy(t *testing.T) {

func TestGetClusterFilterUnknown(t *testing.T) {
shardIndex := 1 // ensuring that a shard with index 1 will process all the clusters with an "even" id (2,4,6,...)
os.Setenv(common.EnvControllerReplicas, "2")
os.Setenv(common.EnvControllerShardingAlgorithm, "unknown")
t.Setenv(common.EnvControllerReplicas, "2")
t.Setenv(common.EnvControllerShardingAlgorithm, "unknown")
filter := GetClusterFilter(GetDistributionFunction(nil, "unknown"), shardIndex)
assert.False(t, filter(&v1alpha1.Cluster{ID: "1"}))
assert.True(t, filter(&v1alpha1.Cluster{ID: "2"}))
Expand All @@ -92,7 +91,7 @@ func TestGetClusterFilterUnknown(t *testing.T) {

func TestLegacyGetClusterFilterWithFixedShard(t *testing.T) {
shardIndex := 1 // ensuring that a shard with index 1 will process all the clusters with an "even" id (2,4,6,...)
os.Setenv(common.EnvControllerReplicas, "2")
t.Setenv(common.EnvControllerReplicas, "2")
filter := GetClusterFilter(GetDistributionFunction(nil, common.DefaultShardingAlgorithm), shardIndex)
assert.False(t, filter(nil))
assert.False(t, filter(&v1alpha1.Cluster{ID: "1"}))
Expand All @@ -107,12 +106,11 @@ func TestLegacyGetClusterFilterWithFixedShard(t *testing.T) {
fixedShard = 1
filter = GetClusterFilter(GetDistributionFunction(nil, common.DefaultShardingAlgorithm), int(fixedShard))
assert.True(t, filter(&v1alpha1.Cluster{Name: "cluster4", ID: "4", Shard: &fixedShard}))

}

func TestRoundRobinGetClusterFilterWithFixedShard(t *testing.T) {
shardIndex := 1 // ensuring that a shard with index 1 will process all the clusters with an "even" id (2,4,6,...)
os.Setenv(common.EnvControllerReplicas, "2")
t.Setenv(common.EnvControllerReplicas, "2")
db, cluster1, cluster2, cluster3, cluster4, _ := createTestClusters()

filter := GetClusterFilter(GetDistributionFunction(db, common.RoundRobinShardingAlgorithm), shardIndex)
Expand All @@ -135,8 +133,8 @@ func TestRoundRobinGetClusterFilterWithFixedShard(t *testing.T) {

func TestGetClusterFilterLegacyHash(t *testing.T) {
shardIndex := 1 // ensuring that a shard with index 1 will process all the clusters with an "even" id (2,4,6,...)
os.Setenv(common.EnvControllerReplicas, "2")
os.Setenv(common.EnvControllerShardingAlgorithm, "hash")
t.Setenv(common.EnvControllerReplicas, "2")
t.Setenv(common.EnvControllerShardingAlgorithm, "hash")
db, cluster1, cluster2, cluster3, cluster4, _ := createTestClusters()
filter := GetClusterFilter(GetDistributionFunction(db, common.LegacyShardingAlgorithm), shardIndex)
assert.False(t, filter(&cluster1))
Expand All @@ -158,55 +156,64 @@ func TestGetClusterFilterLegacyHash(t *testing.T) {
func TestGetClusterFilterWithEnvControllerShardingAlgorithms(t *testing.T) {
db, cluster1, cluster2, cluster3, cluster4, _ := createTestClusters()
shardIndex := 1
os.Setenv(common.EnvControllerReplicas, "2")
os.Setenv(common.EnvControllerShardingAlgorithm, common.LegacyShardingAlgorithm)
shardShouldProcessCluster := GetClusterFilter(GetDistributionFunction(db, common.LegacyShardingAlgorithm), shardIndex)
assert.False(t, shardShouldProcessCluster(&cluster1))
assert.True(t, shardShouldProcessCluster(&cluster2))
assert.False(t, shardShouldProcessCluster(&cluster3))
assert.True(t, shardShouldProcessCluster(&cluster4))
assert.False(t, shardShouldProcessCluster(nil))

os.Setenv(common.EnvControllerShardingAlgorithm, common.RoundRobinShardingAlgorithm)
shardShouldProcessCluster = GetClusterFilter(GetDistributionFunction(db, common.LegacyShardingAlgorithm), shardIndex)
assert.False(t, shardShouldProcessCluster(&cluster1))
assert.True(t, shardShouldProcessCluster(&cluster2))
assert.False(t, shardShouldProcessCluster(&cluster3))
assert.True(t, shardShouldProcessCluster(&cluster4))
assert.False(t, shardShouldProcessCluster(nil))
t.Setenv(common.EnvControllerReplicas, "2")

t.Run("legacy", func(t *testing.T) {
t.Setenv(common.EnvControllerShardingAlgorithm, common.LegacyShardingAlgorithm)
shardShouldProcessCluster := GetClusterFilter(GetDistributionFunction(db, common.LegacyShardingAlgorithm), shardIndex)
assert.False(t, shardShouldProcessCluster(&cluster1))
assert.True(t, shardShouldProcessCluster(&cluster2))
assert.False(t, shardShouldProcessCluster(&cluster3))
assert.True(t, shardShouldProcessCluster(&cluster4))
assert.False(t, shardShouldProcessCluster(nil))
})

t.Run("roundrobin", func(t *testing.T) {
t.Setenv(common.EnvControllerShardingAlgorithm, common.RoundRobinShardingAlgorithm)
shardShouldProcessCluster := GetClusterFilter(GetDistributionFunction(db, common.LegacyShardingAlgorithm), shardIndex)
assert.False(t, shardShouldProcessCluster(&cluster1))
assert.True(t, shardShouldProcessCluster(&cluster2))
assert.False(t, shardShouldProcessCluster(&cluster3))
assert.True(t, shardShouldProcessCluster(&cluster4))
assert.False(t, shardShouldProcessCluster(nil))
})
}

func TestGetShardByIndexModuloReplicasCountDistributionFunction2(t *testing.T) {
db, cluster1, cluster2, cluster3, cluster4, cluster5 := createTestClusters()
// Test with replicas set to 1
os.Setenv(common.EnvControllerReplicas, "1")
distributionFunction := RoundRobinDistributionFunction(db)
assert.Equal(t, 0, distributionFunction(nil))
assert.Equal(t, 0, distributionFunction(&cluster1))
assert.Equal(t, 0, distributionFunction(&cluster2))
assert.Equal(t, 0, distributionFunction(&cluster3))
assert.Equal(t, 0, distributionFunction(&cluster4))
assert.Equal(t, 0, distributionFunction(&cluster5))

// Test with replicas set to 2
os.Setenv(common.EnvControllerReplicas, "2")
distributionFunction = RoundRobinDistributionFunction(db)
assert.Equal(t, 0, distributionFunction(nil))
assert.Equal(t, 0, distributionFunction(&cluster1))
assert.Equal(t, 1, distributionFunction(&cluster2))
assert.Equal(t, 0, distributionFunction(&cluster3))
assert.Equal(t, 1, distributionFunction(&cluster4))
assert.Equal(t, 0, distributionFunction(&cluster5))

// // Test with replicas set to 3
os.Setenv(common.EnvControllerReplicas, "3")
distributionFunction = RoundRobinDistributionFunction(db)
assert.Equal(t, 0, distributionFunction(nil))
assert.Equal(t, 0, distributionFunction(&cluster1))
assert.Equal(t, 1, distributionFunction(&cluster2))
assert.Equal(t, 2, distributionFunction(&cluster3))
assert.Equal(t, 0, distributionFunction(&cluster4))
assert.Equal(t, 1, distributionFunction(&cluster5))
t.Run("replicas set to 1", func(t *testing.T) {
t.Setenv(common.EnvControllerReplicas, "1")
distributionFunction := RoundRobinDistributionFunction(db)
assert.Equal(t, 0, distributionFunction(nil))
assert.Equal(t, 0, distributionFunction(&cluster1))
assert.Equal(t, 0, distributionFunction(&cluster2))
assert.Equal(t, 0, distributionFunction(&cluster3))
assert.Equal(t, 0, distributionFunction(&cluster4))
assert.Equal(t, 0, distributionFunction(&cluster5))
})

t.Run("replicas set to 2", func(t *testing.T) {
t.Setenv(common.EnvControllerReplicas, "2")
distributionFunction := RoundRobinDistributionFunction(db)
assert.Equal(t, 0, distributionFunction(nil))
assert.Equal(t, 0, distributionFunction(&cluster1))
assert.Equal(t, 1, distributionFunction(&cluster2))
assert.Equal(t, 0, distributionFunction(&cluster3))
assert.Equal(t, 1, distributionFunction(&cluster4))
assert.Equal(t, 0, distributionFunction(&cluster5))
})

t.Run("replicas set to 3", func(t *testing.T) {
t.Setenv(common.EnvControllerReplicas, "3")
distributionFunction := RoundRobinDistributionFunction(db)
assert.Equal(t, 0, distributionFunction(nil))
assert.Equal(t, 0, distributionFunction(&cluster1))
assert.Equal(t, 1, distributionFunction(&cluster2))
assert.Equal(t, 2, distributionFunction(&cluster3))
assert.Equal(t, 0, distributionFunction(&cluster4))
assert.Equal(t, 1, distributionFunction(&cluster5))
})
}

func TestGetShardByIndexModuloReplicasCountDistributionFunctionWhenClusterNumberIsHigh(t *testing.T) {
Expand All @@ -222,7 +229,7 @@ func TestGetShardByIndexModuloReplicasCountDistributionFunctionWhenClusterNumber
clusterList.Items = append(clusterList.Items, cluster)
}
db.On("ListClusters", mock.Anything).Return(clusterList, nil)
os.Setenv(common.EnvControllerReplicas, "2")
t.Setenv(common.EnvControllerReplicas, "2")
distributionFunction := RoundRobinDistributionFunction(&db)
for i, c := range clusterList.Items {
assert.Equal(t, i%2, distributionFunction(&c))
Expand All @@ -242,7 +249,7 @@ func TestGetShardByIndexModuloReplicasCountDistributionFunctionWhenClusterIsAdde
db.On("ListClusters", mock.Anything).Return(clusterList, nil)

// Test with replicas set to 2
os.Setenv(common.EnvControllerReplicas, "2")
t.Setenv(common.EnvControllerReplicas, "2")
distributionFunction := RoundRobinDistributionFunction(&db)
assert.Equal(t, 0, distributionFunction(nil))
assert.Equal(t, 0, distributionFunction(&cluster1))
Expand All @@ -259,12 +266,11 @@ func TestGetShardByIndexModuloReplicasCountDistributionFunctionWhenClusterIsAdde
// Now, we remove the last added cluster, it should be unassigned as well
clusterList.Items = clusterList.Items[:len(clusterList.Items)-1]
assert.Equal(t, -1, distributionFunction(&cluster6))

}

func TestGetShardByIndexModuloReplicasCountDistributionFunction(t *testing.T) {
db, cluster1, cluster2, _, _, _ := createTestClusters()
os.Setenv(common.EnvControllerReplicas, "2")
t.Setenv(common.EnvControllerReplicas, "2")
distributionFunction := RoundRobinDistributionFunction(db)

// Test that the function returns the correct shard for cluster1 and cluster2
Expand Down Expand Up @@ -303,7 +309,6 @@ func TestInferShard(t *testing.T) {
osHostnameFunction = func() (string, error) { return "example-shard", nil }
_, err = InferShard()
assert.NotNil(t, err)

}

func createTestClusters() (*dbmocks.ArgoDB, v1alpha1.Cluster, v1alpha1.Cluster, v1alpha1.Cluster, v1alpha1.Cluster, v1alpha1.Cluster) {
Expand Down
5 changes: 2 additions & 3 deletions controller/sharding/shuffle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sharding
import (
"fmt"
"math"
"os"
"testing"

"github.com/argoproj/argo-cd/v2/common"
Expand All @@ -24,7 +23,7 @@ func TestLargeShuffle(t *testing.T) {
}
db.On("ListClusters", mock.Anything).Return(clusterList, nil)
// Test with replicas set to 256
os.Setenv(common.EnvControllerReplicas, "256")
t.Setenv(common.EnvControllerReplicas, "256")
distributionFunction := RoundRobinDistributionFunction(&db)
for i, c := range clusterList.Items {
assert.Equal(t, i%2567, distributionFunction(&c))
Expand All @@ -47,7 +46,7 @@ func TestShuffle(t *testing.T) {
db.On("ListClusters", mock.Anything).Return(clusterList, nil)

// Test with replicas set to 3
os.Setenv(common.EnvControllerReplicas, "3")
t.Setenv(common.EnvControllerReplicas, "3")
distributionFunction := RoundRobinDistributionFunction(&db)
assert.Equal(t, 0, distributionFunction(nil))
assert.Equal(t, 0, distributionFunction(&cluster1))
Expand Down
13 changes: 4 additions & 9 deletions controller/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ func TestAppRevisionsSingleSource(t *testing.T) {
assert.NotNil(t, compRes.syncStatus)
assert.NotEmpty(t, compRes.syncStatus.Revision)
assert.Len(t, compRes.syncStatus.Revisions, 0)

}

// TestAppRevisions tests that revisions are properly propagated for a multi source app
Expand Down Expand Up @@ -708,9 +707,8 @@ var signedProj = argoappv1.AppProject{
}

func TestSignedResponseNoSignatureRequired(t *testing.T) {
oldval := os.Getenv("ARGOCD_GPG_ENABLED")
os.Setenv("ARGOCD_GPG_ENABLED", "true")
defer os.Setenv("ARGOCD_GPG_ENABLED", oldval)
t.Setenv("ARGOCD_GPG_ENABLED", "true")

// We have a good signature response, but project does not require signed commits
{
app := newFakeApp()
Expand Down Expand Up @@ -766,9 +764,7 @@ func TestSignedResponseNoSignatureRequired(t *testing.T) {
}

func TestSignedResponseSignatureRequired(t *testing.T) {
oldval := os.Getenv("ARGOCD_GPG_ENABLED")
os.Setenv("ARGOCD_GPG_ENABLED", "true")
defer os.Setenv("ARGOCD_GPG_ENABLED", oldval)
t.Setenv("ARGOCD_GPG_ENABLED", "true")

// We have a good signature response, valid key, and signing is required - sync!
{
Expand Down Expand Up @@ -934,7 +930,7 @@ func TestSignedResponseSignatureRequired(t *testing.T) {
assert.Contains(t, app.Status.Conditions[0].Message, "Cannot use local manifests")
}

os.Setenv("ARGOCD_GPG_ENABLED", "false")
t.Setenv("ARGOCD_GPG_ENABLED", "false")
// We have a bad signature response and signing would be required, but GPG subsystem is disabled - sync
{
app := newFakeApp()
Expand Down Expand Up @@ -990,7 +986,6 @@ func TestSignedResponseSignatureRequired(t *testing.T) {
assert.Len(t, compRes.managedResources, 0)
assert.Len(t, app.Status.Conditions, 0)
}

}

func TestComparisonResult_GetHealthStatus(t *testing.T) {
Expand Down
Loading