Skip to content

Commit

Permalink
Revert "Remove custom Docker networks from integration tests"
Browse files Browse the repository at this point in the history
This reverts commit 57b5560.
  • Loading branch information
twz123 committed Sep 5, 2024
1 parent 9c1c02b commit 405bb7c
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 3 deletions.
20 changes: 18 additions & 2 deletions inttest/airgap/airgap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ type AirgapSuite struct {
common.BootlooseSuite
}

const network = "airgap"

// SetupSuite creates the required network before starting footloose.
func (s *AirgapSuite) SetupSuite() {
s.Require().NoError(s.CreateNetwork(network))
s.BootlooseSuite.SetupSuite()
}

// TearDownSuite tears down the network created after footloose has finished.
func (s *AirgapSuite) TearDownSuite() {
s.BootlooseSuite.TearDownSuite()
s.Require().NoError(s.MaybeDestroyNetwork(network))
}

func (s *AirgapSuite) TestK0sGetsUp() {
ctx := s.Context()
err := (&common.Airgap{
Expand Down Expand Up @@ -101,8 +115,10 @@ func (s *AirgapSuite) TestK0sGetsUp() {
func TestAirgapSuite(t *testing.T) {
s := AirgapSuite{
common.BootlooseSuite{
ControllerCount: 1,
WorkerCount: 1,
ControllerCount: 1,
WorkerCount: 1,
ControllerNetworks: []string{network},
WorkerNetworks: []string{network},

AirgapImageBundleMountPoints: []string{"/var/lib/k0s/images/bundle.tar"},
},
Expand Down
17 changes: 17 additions & 0 deletions inttest/ap-airgap/airgap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ type airgapSuite struct {
common.BootlooseSuite
}

const network = "ap-airgap"

// SetupSuite creates the required network before starting footloose.
func (s *airgapSuite) SetupSuite() {
s.Require().NoError(s.CreateNetwork(network))
s.BootlooseSuite.SetupSuite()
}

// TearDownSuite tears down the network created after footloose has finished.
func (s *airgapSuite) TearDownSuite() {
s.BootlooseSuite.TearDownSuite()
s.Require().NoError(s.MaybeDestroyNetwork(network))
}

// SetupTest prepares the controller and filesystem, getting it into a consistent
// state which we can run tests against.
func (s *airgapSuite) SetupTest() {
Expand Down Expand Up @@ -214,6 +228,9 @@ func TestAirgapSuite(t *testing.T) {
LaunchMode: common.LaunchModeOpenRC,

AirgapImageBundleMountPoints: []string{"/dist/bundle.tar"},

ControllerNetworks: []string{network},
WorkerNetworks: []string{network},
},
})
}
17 changes: 17 additions & 0 deletions inttest/ap-ha3x3/ha3x3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ spec:
externalAddress: %s
`

const network = "ha3x3net"

// SetupSuite creates the required network before starting footloose.
func (s *ha3x3Suite) SetupSuite() {
s.Require().NoError(s.CreateNetwork(network))
s.BootlooseSuite.SetupSuite()
}

// TearDownSuite tears down the network created after footloose has finished.
func (s *ha3x3Suite) TearDownSuite() {
s.BootlooseSuite.TearDownSuite()
s.Require().NoError(s.MaybeDestroyNetwork(network))
}

// SetupTest prepares the controller and filesystem, getting it into a consistent
// state which we can run tests against.
func (s *ha3x3Suite) SetupTest() {
Expand Down Expand Up @@ -292,6 +306,9 @@ func TestHA3x3Suite(t *testing.T) {
WorkerCount: 3,
WithLB: true,
LaunchMode: common.LaunchModeOpenRC,

ControllerNetworks: []string{network},
WorkerNetworks: []string{network},
},
k0sUpdateVersion,
})
Expand Down
17 changes: 17 additions & 0 deletions inttest/ap-quorum/quorum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ spec:
externalAddress: %s
`

const network = "quorumnet"

// SetupSuite creates the required network before starting footloose.
func (s *quorumSuite) SetupSuite() {
s.Require().NoError(s.CreateNetwork(network))
s.BootlooseSuite.SetupSuite()
}

// TearDownSuite tears down the network created after footloose has finished.
func (s *quorumSuite) TearDownSuite() {
s.BootlooseSuite.TearDownSuite()
s.Require().NoError(s.MaybeDestroyNetwork(network))
}

// SetupTest prepares the controller and filesystem, getting it into a consistent
// state which we can run tests against.
func (s *quorumSuite) SetupTest() {
Expand Down Expand Up @@ -138,6 +152,9 @@ func TestQuorumSuite(t *testing.T) {
ControllerCount: 3,
WorkerCount: 0,
LaunchMode: common.LaunchModeOpenRC,

ControllerNetworks: []string{network},
WorkerNetworks: []string{network},
},
})
}
17 changes: 17 additions & 0 deletions inttest/ap-quorumsafety/quorumsafety_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ spec:
externalAddress: %s
`

const network = "quorumsafetynet"

// SetupSuite creates the required network before starting footloose.
func (s *quorumSafetySuite) SetupSuite() {
s.Require().NoError(s.CreateNetwork(network))
s.BootlooseSuite.SetupSuite()
}

// TearDownSuite tears down the network created after footloose has finished.
func (s *quorumSafetySuite) TearDownSuite() {
s.BootlooseSuite.TearDownSuite()
s.Require().NoError(s.MaybeDestroyNetwork(network))
}

// SetupTest prepares the controller and filesystem, getting it into a consistent
// state which we can run tests against.
func (s *quorumSafetySuite) SetupTest() {
Expand Down Expand Up @@ -149,6 +163,9 @@ func TestQuorumSafetySuite(t *testing.T) {
ControllerCount: 2,
WorkerCount: 0,
LaunchMode: common.LaunchModeOpenRC,

ControllerNetworks: []string{network},
WorkerNetworks: []string{network},
},
})
}
14 changes: 14 additions & 0 deletions inttest/ap-selector/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ spec:
externalAddress: %s
`

const network = "selectornet"

// SetupSuite creates the required network before starting footloose.
func (s *selectorSuite) SetupSuite() {
s.Require().NoError(s.CreateNetwork(network))
s.BootlooseSuite.SetupSuite()
}

// TearDownSuite tears down the network created after footloose has finished.
func (s *selectorSuite) TearDownSuite() {
s.BootlooseSuite.TearDownSuite()
s.Require().NoError(s.MaybeDestroyNetwork(network))
}

// SetupTest prepares the controller and filesystem, getting it into a consistent
// state which we can run tests against.
func (s *selectorSuite) SetupTest() {
Expand Down
49 changes: 48 additions & 1 deletion inttest/common/bootloosesuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ type BootlooseSuite struct {
K0smotronWorkerCount int
WithUpdateServer bool
BootLooseImage string
ControllerNetworks []string
WorkerNetworks []string
K0smotronNetworks []string

ctx context.Context
tearDown func()
Expand Down Expand Up @@ -615,7 +618,13 @@ func (s *BootlooseSuite) getControllersIPAddresses() []string {
s.Require().NoError(err)

for i := 0; i < s.ControllerCount; i++ {
addresses[i] = machines[i].Status().IP
// If a network is supplied, the address will need to be obtained from there.
// Note that this currently uses the first network found.
if machines[i].Status().IP != "" {
addresses[i] = machines[i].Status().IP
} else if len(machines[i].Status().RuntimeNetworks) > 0 {
addresses[i] = machines[i].Status().RuntimeNetworks[0].IP
}
}
return addresses
}
Expand Down Expand Up @@ -1175,6 +1184,7 @@ func (s *BootlooseSuite) initializeBootlooseClusterInDir(dir string) error {
Privileged: true,
Volumes: volumes,
PortMappings: portMaps,
Networks: s.ControllerNetworks,
},
},
{
Expand All @@ -1185,6 +1195,7 @@ func (s *BootlooseSuite) initializeBootlooseClusterInDir(dir string) error {
Privileged: true,
Volumes: volumes,
PortMappings: portMaps,
Networks: s.WorkerNetworks,
},
},
{
Expand All @@ -1195,6 +1206,7 @@ func (s *BootlooseSuite) initializeBootlooseClusterInDir(dir string) error {
Privileged: true,
Volumes: volumes,
PortMappings: portMaps,
Networks: s.K0smotronNetworks,
},
},
},
Expand All @@ -1208,6 +1220,7 @@ func (s *BootlooseSuite) initializeBootlooseClusterInDir(dir string) error {
Privileged: true,
Volumes: volumes,
PortMappings: portMaps,
Networks: s.ControllerNetworks,
},
Count: 1,
})
Expand Down Expand Up @@ -1239,6 +1252,7 @@ func (s *BootlooseSuite) initializeBootlooseClusterInDir(dir string) error {
ContainerPort: 80,
},
},
Networks: s.ControllerNetworks,
},
Count: 1,
})
Expand Down Expand Up @@ -1324,6 +1338,39 @@ func (s *BootlooseSuite) GetIPAddress(nodeName string) string {
return ipAddress
}

// CreateNetwork creates a docker network with the provided name, destroying
// any network that has the same name first.
func (s *BootlooseSuite) CreateNetwork(name string) error {
// Don't create the network if it already exists as it might be in use from previous tests
// ran with K0S_KEEP_AFTER_TEST variable set.
if s.NetworkExists(name) {
return nil
}

cmd := exec.Command("docker", "network", "create", name)
return cmd.Run()
}

// MaybeDestroyNetwork removes a docker network with the provided name.
// The network might not get removed if there's still containers attached to it.
// This is the case for example when running the tests with K0S_KEEP_AFTER_TEST=always.
func (s *BootlooseSuite) MaybeDestroyNetwork(name string) error {
// If we're supposed to leave the footloose containers running, don't try to destroy the network
if keepEnvironment(s.T()) {
return nil
}
cmd := exec.Command("docker", "network", "rm", name)
return cmd.Run()
}

// NetworkExists returns true if a docker network with the provided name exists.
func (s *BootlooseSuite) NetworkExists(name string) bool {
cmd := exec.Command("docker", "network", "inspect", name)
err := cmd.Run()

return err == nil
}

// RunCommandController runs a command via SSH on a specified controller node
func (s *BootlooseSuite) RunCommandController(idx int, command string) (string, error) {
ssh, err := s.SSH(s.Context(), s.ControllerNode(idx))
Expand Down
17 changes: 17 additions & 0 deletions inttest/kubeletcertrotate/kubeletcertrotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ type kubeletCertRotateSuite struct {
common.BootlooseSuite
}

const network = "kubeletcertrotatenet"

// SetupSuite creates the required network before starting footloose.
func (s *kubeletCertRotateSuite) SetupSuite() {
s.Require().NoError(s.CreateNetwork(network))
s.BootlooseSuite.SetupSuite()
}

// TearDownSuite tears down the network created after footloose has finished.
func (s *kubeletCertRotateSuite) TearDownSuite() {
s.BootlooseSuite.TearDownSuite()
s.Require().NoError(s.MaybeDestroyNetwork(network))
}

type statusJSON struct {
WorkerToAPIConnectionStatus status.ProbeStatus
}
Expand Down Expand Up @@ -175,6 +189,9 @@ func TestKubeletCertRotateSuite(t *testing.T) {
ControllerCount: 1,
WorkerCount: 1,
LaunchMode: common.LaunchModeOpenRC,

ControllerNetworks: []string{network},
WorkerNetworks: []string{network},
},
})
}

0 comments on commit 405bb7c

Please sign in to comment.