Skip to content

Commit

Permalink
Replace kubectl apply usage in inttests
Browse files Browse the repository at this point in the history
This removes SSH and "shelling out" from the equation, making the tests
a bit more direct and hopefully improving error messages when things
go south.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Sep 23, 2024
1 parent 1ead5bf commit 3ae3594
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 122 deletions.
15 changes: 7 additions & 8 deletions inttest/ap-airgap/airgap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
appc "github.com/k0sproject/k0s/pkg/autopilot/controller/plans/core"
k0sclientset "github.com/k0sproject/k0s/pkg/client/clientset"
"github.com/k0sproject/k0s/pkg/constant"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -142,20 +143,18 @@ spec:
)
s.Require().NoError(err)

client, err := s.AutopilotClient(s.ControllerNode(0))
restConfig, err := s.GetKubeConfig(s.ControllerNode(0))
s.Require().NoError(err)
s.NotEmpty(client)

manifestFile := "/tmp/happy.yaml"
s.PutFileTemplate(s.ControllerNode(0), manifestFile, planTemplate, nil)
err = common.Create(ctx, restConfig, []byte(planTemplate))
s.Require().NoError(err)
s.T().Logf("Plan created")

updateStart := time.Now()

out, err := s.RunCommandController(0, fmt.Sprintf("/usr/local/bin/k0s kubectl apply -f %s", manifestFile))
s.T().Logf("kubectl apply output: '%s'", out)
s.Require().NoError(err)

// The plan has enough information to perform a successful update of k0s, so wait for it.
client, err := k0sclientset.NewForConfig(restConfig)
s.Require().NoError(err)
_, err = aptest.WaitForPlanState(ctx, client, apconst.AutopilotName, appc.PlanCompleted)
s.Require().NoError(err)

Expand Down
22 changes: 11 additions & 11 deletions inttest/ap-controllerworker/controllerworker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import (

"github.com/k0sproject/k0s/inttest/common"
aptest "github.com/k0sproject/k0s/inttest/common/autopilot"

apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
appc "github.com/k0sproject/k0s/pkg/autopilot/controller/plans/core"
k0sclientset "github.com/k0sproject/k0s/pkg/client/clientset"
"github.com/k0sproject/k0s/pkg/constant"
"github.com/k0sproject/k0s/pkg/kubernetes/watch"

"github.com/stretchr/testify/suite"

corev1 "k8s.io/api/core/v1"

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

type controllerworkerSuite struct {
Expand Down Expand Up @@ -114,9 +114,10 @@ func (s *controllerworkerSuite) SetupTest() {
// TestApply applies a well-formed `plan` yaml, and asserts that
// all of the correct values across different objects + controllers are correct.
func (s *controllerworkerSuite) TestApply() {
client, err := s.AutopilotClient(s.ControllerNode(0))
ctx := s.Context()

restConfig, err := s.GetKubeConfig(s.ControllerNode(0))
s.Require().NoError(err)
s.NotEmpty(client)

planTemplate := `
apiVersion: autopilot.k0sproject.io/v1beta2
Expand Down Expand Up @@ -144,16 +145,15 @@ spec:
- controller2
- controller0
`
ctx := s.Context()
manifestFile := "/tmp/happy.yaml"
s.PutFileTemplate(s.ControllerNode(0), manifestFile, planTemplate, nil)

out, err := s.RunCommandController(0, fmt.Sprintf("/usr/local/bin/k0s kubectl apply -f %s", manifestFile))
s.T().Logf("kubectl apply output: '%s'", out)
err = common.Create(ctx, restConfig, []byte(planTemplate))
s.Require().NoError(err)
s.T().Logf("Plan created")

// The plan has enough information to perform a successful update of k0s, so wait for it.
plan, err := aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanCompleted)
client, err := k0sclientset.NewForConfig(restConfig)
s.Require().NoError(err)
plan, err := aptest.WaitForPlanState(ctx, client, apconst.AutopilotName, appc.PlanCompleted)
s.Require().NoError(err)

s.Equal(1, len(plan.Status.Commands))
Expand Down
18 changes: 9 additions & 9 deletions inttest/ap-platformselect/platformselect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package platformselect

import (
"fmt"
"testing"
"time"

apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
appc "github.com/k0sproject/k0s/pkg/autopilot/controller/plans/core"
k0sclientset "github.com/k0sproject/k0s/pkg/client/clientset"

"github.com/k0sproject/k0s/inttest/common"
aptest "github.com/k0sproject/k0s/inttest/common/autopilot"
Expand Down Expand Up @@ -51,9 +51,10 @@ func (s *platformSelectSuite) SetupTest() {
// TestApply applies a well-formed `plan` yaml that includes multiple
// platform definitions, and asserts that the proper binary is downloaded.
func (s *platformSelectSuite) TestApply() {
client, err := s.AutopilotClient(s.ControllerNode(0))
ctx := s.Context()

restConfig, err := s.GetKubeConfig(s.ControllerNode(0))
s.Require().NoError(err)
s.NotEmpty(client)

planTemplate := `
apiVersion: autopilot.k0sproject.io/v1beta2
Expand Down Expand Up @@ -82,18 +83,17 @@ spec:
- controller0
`

manifestFile := "/tmp/happy.yaml"
s.PutFileTemplate(s.ControllerNode(0), manifestFile, planTemplate, nil)

out, err := s.RunCommandController(0, fmt.Sprintf("/usr/local/bin/k0s kubectl apply -f %s", manifestFile))
s.T().Logf("kubectl apply output: '%s'", out)
err = common.Create(ctx, restConfig, []byte(planTemplate))
s.Require().NoError(err)
s.T().Logf("Plan created")

// Its expected that if the wrong platform were to be downloaded, the update wouldn't be successful,
// as the binary would fail to run.

// The plan has enough information to perform a successful update of k0s, so wait for it.
plan, err := aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanCompleted)
client, err := k0sclientset.NewForConfig(restConfig)
s.Require().NoError(err)
plan, err := aptest.WaitForPlanState(ctx, client, apconst.AutopilotName, appc.PlanCompleted)
s.Require().NoError(err)

s.Equal(1, len(plan.Status.Commands))
Expand Down
17 changes: 9 additions & 8 deletions inttest/ap-quorum/quorum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
appc "github.com/k0sproject/k0s/pkg/autopilot/controller/plans/core"
k0sclientset "github.com/k0sproject/k0s/pkg/client/clientset"

"github.com/k0sproject/k0s/inttest/common"
aptest "github.com/k0sproject/k0s/inttest/common/autopilot"
Expand Down Expand Up @@ -77,9 +78,10 @@ func (s *quorumSuite) SetupTest() {
// TestApply applies a well-formed `plan` yaml, and asserts that
// all of the correct values across different objects + controllers are correct.
func (s *quorumSuite) TestApply() {
client, err := s.AutopilotClient(s.ControllerNode(0))
ctx := s.Context()

restConfig, err := s.GetKubeConfig(s.ControllerNode(0))
s.Require().NoError(err)
s.NotEmpty(client)

planTemplate := `
apiVersion: autopilot.k0sproject.io/v1beta2
Expand All @@ -106,15 +108,14 @@ spec:
- controller2
`

manifestFile := "/tmp/happy.yaml"
s.PutFileTemplate(s.ControllerNode(0), manifestFile, planTemplate, nil)

out, err := s.RunCommandController(0, fmt.Sprintf("/usr/local/bin/k0s kubectl apply -f %s", manifestFile))
s.T().Logf("kubectl apply output: '%s'", out)
err = common.Create(ctx, restConfig, []byte(planTemplate))
s.Require().NoError(err)
s.T().Logf("Plan created")

// The plan has enough information to perform a successful update of k0s, so wait for it.
plan, err := aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanCompleted)
client, err := k0sclientset.NewForConfig(restConfig)
s.Require().NoError(err)
plan, err := aptest.WaitForPlanState(ctx, client, apconst.AutopilotName, appc.PlanCompleted)
s.Require().NoError(err)

s.Equal(1, len(plan.Status.Commands))
Expand Down
27 changes: 13 additions & 14 deletions inttest/ap-quorumsafety/quorumsafety_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
appc "github.com/k0sproject/k0s/pkg/autopilot/controller/plans/core"
k0sclientset "github.com/k0sproject/k0s/pkg/client/clientset"

"github.com/k0sproject/k0s/inttest/common"
aptest "github.com/k0sproject/k0s/inttest/common/autopilot"
Expand Down Expand Up @@ -77,6 +78,10 @@ func (s *quorumSafetySuite) SetupTest() {
// TestApply applies a well-formed `plan` yaml, and asserts that
// all of the correct values across different objects + controllers are correct.
func (s *quorumSafetySuite) TestApply() {
ctx := s.Context()

restConfig, err := s.GetKubeConfig(s.ControllerNode(0))
s.Require().NoError(err)

// Create a third node by way of a new `ControlNode` entry that doesen't map to a host.
// This will allow autopilot to get past the node tests in newplan (IncompleteTargets)
Expand All @@ -91,18 +96,13 @@ metadata:
kubernetes.io/hostname: controller2
kubernetes.io/os: linux
`
controller2Filename := "/tmp/controller2.yaml"
s.PutFile(s.ControllerNode(0), controller2Filename, controller2Def)
out, err := s.RunCommandController(0, fmt.Sprintf("/usr/local/bin/k0s kubectl apply -f %s", controller2Filename))
s.T().Logf("kubectl apply output (controller2): '%s'", out)

err = common.Create(ctx, restConfig, []byte(controller2Def))
s.Require().NoError(err)
s.T().Logf("Second ControlNode created")

// Create + populate the plan

client, err := s.AutopilotClient(s.ControllerNode(0))
s.Require().NoError(err)
s.NotEmpty(client)

planTemplate := `
apiVersion: autopilot.k0sproject.io/v1beta2
kind: Plan
Expand All @@ -128,16 +128,15 @@ spec:
- controller2
`

manifestFile := "/tmp/happy.yaml"
s.PutFileTemplate(s.ControllerNode(0), manifestFile, planTemplate, nil)

out, err = s.RunCommandController(0, fmt.Sprintf("/usr/local/bin/k0s kubectl apply -f %s", manifestFile))
s.T().Logf("kubectl apply output (plan): '%s'", out)
err = common.Create(ctx, restConfig, []byte(planTemplate))
s.Require().NoError(err)
s.T().Logf("Plan created")

// The plan should fail with "InconsistentTargets" due to autopilot detecting that `controller2`
// despite existing as a `ControlNode`, does not resolve.
_, err = aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanInconsistentTargets)
client, err := k0sclientset.NewForConfig(restConfig)
s.Require().NoError(err)
_, err = aptest.WaitForPlanState(ctx, client, apconst.AutopilotName, appc.PlanInconsistentTargets)
s.Require().NoError(err)
}

Expand Down
16 changes: 7 additions & 9 deletions inttest/ap-removedapis/removedapis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package removedapis

import (
"fmt"
"testing"

apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
appc "github.com/k0sproject/k0s/pkg/autopilot/controller/plans/core"
k0sclientset "github.com/k0sproject/k0s/pkg/client/clientset"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1client "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
Expand Down Expand Up @@ -79,20 +79,18 @@ func (s *plansRemovedAPIsSuite) SetupTest() {
// TestApply applies a well-formed `plan` yaml, and asserts that all of the correct values
// across different objects are correct.
func (s *plansRemovedAPIsSuite) TestApply() {
client, err := s.AutopilotClient(s.ControllerNode(0))
s.Require().NoError(err)
s.NotEmpty(client)

ctx := s.Context()

manifestFile := "/tmp/plan.yaml"
s.PutFileTemplate(s.ControllerNode(0), manifestFile, planTemplate, nil)
restConfig, err := s.GetKubeConfig(s.ControllerNode(0))
s.Require().NoError(err)

out, err := s.RunCommandController(0, fmt.Sprintf("/usr/local/bin/k0s kubectl apply -f %s", manifestFile))
s.T().Logf("kubectl apply output: '%s'", out)
err = common.Create(ctx, restConfig, []byte(planTemplate))
s.Require().NoError(err)
s.T().Logf("Plan created")

// The plan has enough information to perform a successful update of k0s, so wait for it.
client, err := k0sclientset.NewForConfig(restConfig)
s.Require().NoError(err)
plan, err := aptest.WaitForPlanState(ctx, client, apconst.AutopilotName, appc.PlanWarning)
if s.NoError(err) && s.Len(plan.Status.Commands, 1) {
s.Equal(appc.PlanWarning, plan.Status.Commands[0].State)
Expand Down
25 changes: 13 additions & 12 deletions inttest/ap-selector/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
apv1beta2 "github.com/k0sproject/k0s/pkg/apis/autopilot/v1beta2"
apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
appc "github.com/k0sproject/k0s/pkg/autopilot/controller/plans/core"
k0sclientset "github.com/k0sproject/k0s/pkg/client/clientset"

"github.com/k0sproject/k0s/inttest/common"
aptest "github.com/k0sproject/k0s/inttest/common/autopilot"
Expand Down Expand Up @@ -92,6 +93,11 @@ func (s *selectorSuite) SetupTest() {
// TestSelectors applies a well-formed `plan` yaml that wants to only update a controller statically, and
// a worker via field/label selector definitions.
func (s *selectorSuite) TestSelectors() {
ctx := s.Context()

restConfig, err := s.GetKubeConfig(s.ControllerNode(0))
s.Require().NoError(err)

planTemplate := `
apiVersion: autopilot.k0sproject.io/v1beta2
kind: Plan
Expand Down Expand Up @@ -120,26 +126,21 @@ spec:
fields: metadata.name=worker1
`
// Add 'foo=bar' to both 'controller0' and 'worker1'
_, err := s.RunCommandController(0, "/usr/local/bin/k0s kubectl label controlnodes controller0 foo=bar")
_, err = s.RunCommandController(0, "/usr/local/bin/k0s kubectl label controlnodes controller0 foo=bar")
s.Require().NoError(err)
_, err = s.RunCommandController(0, "/usr/local/bin/k0s kubectl label nodes worker1 foo=bar")
s.Require().NoError(err)

// Save + apply the plan
// Create the plan

apc, err := s.AutopilotClient(s.ControllerNode(0))
s.Require().NoError(err)
s.NotEmpty(apc)

manifestFile := "/tmp/plan.yaml"
s.PutFileTemplate(s.ControllerNode(0), manifestFile, planTemplate, nil)

out, err := s.RunCommandController(0, fmt.Sprintf("/usr/local/bin/k0s kubectl apply -f %s", manifestFile))
s.T().Logf("kubectl apply output: '%s'", out)
err = common.Create(ctx, restConfig, []byte(planTemplate))
s.Require().NoError(err)
s.T().Logf("Plan created")

// The plan has enough information to perform a successful update of k0s, so wait for it.
plan, err := aptest.WaitForPlanState(s.Context(), apc, apconst.AutopilotName, appc.PlanCompleted)
client, err := k0sclientset.NewForConfig(restConfig)
s.Require().NoError(err)
plan, err := aptest.WaitForPlanState(ctx, client, apconst.AutopilotName, appc.PlanCompleted)
s.Require().NoError(err)

s.Equal(1, len(plan.Status.Commands))
Expand Down
18 changes: 9 additions & 9 deletions inttest/ap-single/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package single

import (
"fmt"
"testing"

apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
appc "github.com/k0sproject/k0s/pkg/autopilot/controller/plans/core"
k0sclientset "github.com/k0sproject/k0s/pkg/client/clientset"

"github.com/k0sproject/k0s/inttest/common"
aptest "github.com/k0sproject/k0s/inttest/common/autopilot"
Expand Down Expand Up @@ -61,9 +61,10 @@ func (s *plansSingleControllerSuite) SetupTest() {
// TestApply applies a well-formed `plan` yaml, and asserts that all of the correct values
// across different objects are correct.
func (s *plansSingleControllerSuite) TestApply() {
client, err := s.AutopilotClient(s.ControllerNode(0))
ctx := s.Context()

restConfig, err := s.GetKubeConfig(s.ControllerNode(0))
s.Require().NoError(err)
s.NotEmpty(client)

planTemplate := `
apiVersion: autopilot.k0sproject.io/v1beta2
Expand All @@ -88,15 +89,14 @@ spec:
- controller0
`

manifestFile := "/tmp/happy.yaml"
s.PutFileTemplate(s.ControllerNode(0), manifestFile, planTemplate, nil)

out, err := s.RunCommandController(0, fmt.Sprintf("/usr/local/bin/k0s kubectl apply -f %s", manifestFile))
s.T().Logf("kubectl apply output: '%s'", out)
err = common.Create(ctx, restConfig, []byte(planTemplate))
s.Require().NoError(err)
s.T().Logf("Plan created")

// The plan has enough information to perform a successful update of k0s, so wait for it.
plan, err := aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanCompleted)
client, err := k0sclientset.NewForConfig(restConfig)
s.Require().NoError(err)
plan, err := aptest.WaitForPlanState(ctx, client, apconst.AutopilotName, appc.PlanCompleted)
s.Require().NoError(err, "While waiting for plan to complete")

s.Equal(1, len(plan.Status.Commands))
Expand Down
Loading

0 comments on commit 3ae3594

Please sign in to comment.