talos/pkg/resources/network/condition_test.go
Andrey Smirnov 02bd657b25 feat: implement network.Status resource and controller
This resource holds aggregated network status which can be easily used
in various places to wait for the network to reach some desired state.

The state checks are simple right now, we might improve the logic to
make sure all the configured network subsystems reached defined state,
but this might come later as we refine the logic (e.g. to make sure that
all static configuration got applied, etc.)

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2021-06-09 11:10:08 -07:00

84 lines
2.0 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package network_test
import (
"context"
"errors"
"testing"
"time"
"github.com/cosi-project/runtime/pkg/state"
"github.com/cosi-project/runtime/pkg/state/impl/inmem"
"github.com/cosi-project/runtime/pkg/state/impl/namespaced"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/talos-systems/talos/pkg/resources/network"
)
func TestCondition(t *testing.T) {
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(ctxCancel)
t.Parallel()
for _, tt := range []struct {
Name string
Status network.StatusSpec
Checks []network.StatusCheck
Succeeds bool
}{
{
Name: "no checks",
Succeeds: true,
},
{
Name: "timeout",
Checks: []network.StatusCheck{network.AddressReady, network.ConnectivityReady},
Succeeds: false,
},
{
Name: "partial",
Status: network.StatusSpec{
AddressReady: true,
},
Checks: []network.StatusCheck{network.AddressReady, network.ConnectivityReady},
Succeeds: false,
},
{
Name: "okay",
Status: network.StatusSpec{
AddressReady: true,
ConnectivityReady: true,
EtcFilesReady: true,
},
Checks: []network.StatusCheck{network.AddressReady, network.ConnectivityReady},
Succeeds: true,
},
} {
tt := tt
t.Run(tt.Name, func(t *testing.T) {
t.Parallel()
state := state.WrapCore(namespaced.NewState(inmem.Build))
status := network.NewStatus(network.NamespaceName, network.StatusID)
*status.TypedSpec() = tt.Status
require.NoError(t, state.Create(ctx, status))
err := network.NewReadyCondition(state, tt.Checks...).Wait(ctx)
if tt.Succeeds {
assert.NoError(t, err)
} else {
assert.True(t, errors.Is(err, context.DeadlineExceeded), "error is %v", err)
}
})
}
}