Skip to content

Commit

Permalink
Add versioned pod number validation
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Troshin <[email protected]>
  • Loading branch information
antontroshin committed Nov 5, 2024
1 parent 1152a1e commit 5446171
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 8 deletions.
55 changes: 47 additions & 8 deletions tests/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
core_v1 "k8s.io/api/core/v1"
Expand All @@ -46,14 +47,18 @@ const (
ClusterRoles
ClusterRoleBindings

numHAPods = 16
numNonHAPods = 6
numHAPodsWithScheduler = 16
numHAPodsOld = 13
numNonHAPodsWithScheduler = 6
numNonHAPodsOld = 5

thirdPartyDevNamespace = "default"
devRedisReleaseName = "dapr-dev-redis"
devZipkinReleaseName = "dapr-dev-zipkin"
)

var VersionWithScheduler = semver.MustParse("1.14.0")

type VersionDetails struct {
RuntimeVersion string
DashboardVersion string
Expand Down Expand Up @@ -131,7 +136,7 @@ func UpgradeTest(details VersionDetails, opts TestOptions) func(t *testing.T) {
done := make(chan struct{})
podsRunning := make(chan struct{})

go waitAllPodsRunning(t, DaprTestNamespace, opts.HAEnabled, done, podsRunning)
go waitAllPodsRunning(t, DaprTestNamespace, opts.HAEnabled, done, podsRunning, details)
select {
case <-podsRunning:
t.Logf("verified all pods running in namespace %s are running after upgrade", DaprTestNamespace)
Expand Down Expand Up @@ -544,7 +549,7 @@ func GenerateNewCertAndRenew(details VersionDetails, opts TestOptions) func(t *t
done := make(chan struct{})
podsRunning := make(chan struct{})

go waitAllPodsRunning(t, DaprTestNamespace, opts.HAEnabled, done, podsRunning)
go waitAllPodsRunning(t, DaprTestNamespace, opts.HAEnabled, done, podsRunning, details)
select {
case <-podsRunning:
t.Logf("verified all pods running in namespace %s are running after certficate change", DaprTestNamespace)
Expand Down Expand Up @@ -575,7 +580,7 @@ func UseProvidedPrivateKeyAndRenewCerts(details VersionDetails, opts TestOptions
done := make(chan struct{})
podsRunning := make(chan struct{})

go waitAllPodsRunning(t, DaprTestNamespace, opts.HAEnabled, done, podsRunning)
go waitAllPodsRunning(t, DaprTestNamespace, opts.HAEnabled, done, podsRunning, details)
select {
case <-podsRunning:
t.Logf("verified all pods running in namespace %s are running after certficate change", DaprTestNamespace)
Expand Down Expand Up @@ -608,7 +613,7 @@ func UseProvidedNewCertAndRenew(details VersionDetails, opts TestOptions) func(t
done := make(chan struct{})
podsRunning := make(chan struct{})

go waitAllPodsRunning(t, DaprTestNamespace, opts.HAEnabled, done, podsRunning)
go waitAllPodsRunning(t, DaprTestNamespace, opts.HAEnabled, done, podsRunning, details)
select {
case <-podsRunning:
t.Logf("verified all pods running in namespace %s are running after certficate change", DaprTestNamespace)
Expand Down Expand Up @@ -1164,7 +1169,7 @@ func waitPodDeletion(t *testing.T, done, podsDeleted chan struct{}) {
}
}

func waitAllPodsRunning(t *testing.T, namespace string, haEnabled bool, done, podsRunning chan struct{}) {
func waitAllPodsRunning(t *testing.T, namespace string, haEnabled bool, done, podsRunning chan struct{}, details VersionDetails) {
for {
select {
case <-done: // if timeout was reached.
Expand Down Expand Up @@ -1198,14 +1203,48 @@ func waitAllPodsRunning(t *testing.T, namespace string, haEnabled bool, done, po
}
}
}
if len(list.Items) == countOfReadyPods && ((haEnabled && countOfReadyPods == numHAPods) || (!haEnabled && countOfReadyPods == numNonHAPods)) {
pods, err := getVersionedNumberOfPods(haEnabled, details)
if err != nil {
t.Error(err)
}
if len(list.Items) == countOfReadyPods && ((haEnabled && countOfReadyPods == pods) || (!haEnabled && countOfReadyPods == pods)) {
podsRunning <- struct{}{}
}

time.Sleep(15 * time.Second)
}
}

func getVersionedNumberOfPods(isHAEnabled bool, details VersionDetails) (int, error) {
if isHAEnabled {
if details.UseDaprLatestVersion {
return numHAPodsWithScheduler, nil
}
rv, err := semver.NewVersion(details.RuntimeVersion)
if err != nil {
return 0, err
}

if rv.LessThan(VersionWithScheduler) {
return numHAPodsOld, nil
}
return numHAPodsWithScheduler, nil
} else {
if details.UseDaprLatestVersion {
return numNonHAPodsWithScheduler, nil
}
rv, err := semver.NewVersion(details.RuntimeVersion)
if err != nil {
return 0, err
}

if rv.LessThan(VersionWithScheduler) {
return numNonHAPodsOld, nil
}
return numNonHAPodsWithScheduler, nil
}
}

func exportCurrentCertificate(daprPath string) error {
_, err := os.Stat("./certs")
if err != nil {
Expand Down
113 changes: 113 additions & 0 deletions tests/e2e/common/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright 2024 The Dapr Authors
Licensed 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 common

import (
"testing"

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

func TestGetVersionedNumberOfPods(t *testing.T) {
tests := []struct {
name string
isHAEnabled bool
details VersionDetails
expectedNumber int
expectedError bool
}{
{
name: "HA enabled with latest version",
isHAEnabled: true,
details: VersionDetails{UseDaprLatestVersion: true},
expectedNumber: numHAPodsWithScheduler,
expectedError: false,
},
{
name: "HA enabled with old version",
isHAEnabled: true,
details: VersionDetails{UseDaprLatestVersion: false, RuntimeVersion: "1.13.0"},
expectedNumber: numHAPodsOld,
expectedError: false,
},
{
name: "HA disabled with latest version",
isHAEnabled: false,
details: VersionDetails{UseDaprLatestVersion: true},
expectedNumber: numNonHAPodsWithScheduler,
expectedError: false,
},
{
name: "HA disabled with old version",
isHAEnabled: false,
details: VersionDetails{UseDaprLatestVersion: false, RuntimeVersion: "1.13.0"},
expectedNumber: numNonHAPodsOld,
expectedError: false,
},
{
name: "HA enabled with new version",
isHAEnabled: true,
details: VersionDetails{UseDaprLatestVersion: false, RuntimeVersion: "1.14.4"},
expectedNumber: numHAPodsWithScheduler,
expectedError: false,
},
{
name: "HA disabled with new version",
isHAEnabled: false,
details: VersionDetails{UseDaprLatestVersion: false, RuntimeVersion: "1.14.4"},
expectedNumber: numNonHAPodsWithScheduler,
expectedError: false,
},
{
name: "HA enabled with invalid version",
isHAEnabled: true,
details: VersionDetails{UseDaprLatestVersion: false, RuntimeVersion: "invalid version"},
expectedNumber: 0,
expectedError: true,
},
{
name: "HA disabled with invalid version",
isHAEnabled: false,
details: VersionDetails{UseDaprLatestVersion: false, RuntimeVersion: "invalid version"},
expectedNumber: 0,
expectedError: true,
},
{
name: "HA enabled with new RC version",
isHAEnabled: true,
details: VersionDetails{UseDaprLatestVersion: false, RuntimeVersion: "1.15.0-rc.1"},
expectedNumber: numHAPodsWithScheduler,
expectedError: false,
},
{
name: "HA disabled with new RC version",
isHAEnabled: false,
details: VersionDetails{UseDaprLatestVersion: false, RuntimeVersion: "1.15.0-rc.1"},
expectedNumber: numNonHAPodsWithScheduler,
expectedError: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
number, err := getVersionedNumberOfPods(tc.isHAEnabled, tc.details)
if tc.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedNumber, number)
}
})
}
}

0 comments on commit 5446171

Please sign in to comment.