This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
/
environment.go
78 lines (63 loc) · 2.47 KB
/
environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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 tests
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/talos-systems/go-procfs/procfs"
"github.com/talos-systems/go-retry/retry"
"github.com/talos-systems/sidero/app/metal-controller-manager/api/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/talos-systems/sfyra/pkg/talos"
)
const environmentName = "default"
// TestEnvironmentDefault verifies default environment creation.
func TestEnvironmentDefault(ctx context.Context, metalClient client.Client, cluster talos.Cluster, kernelURL, initrdURL string) TestFunc {
return func(t *testing.T) {
var environment v1alpha1.Environment
if err := metalClient.Get(ctx, types.NamespacedName{Name: environmentName}, &environment); err != nil {
if !apierrors.IsNotFound(err) {
require.NoError(t, err)
}
cmdline := procfs.NewDefaultCmdline()
cmdline.Append("console", "ttyS0")
cmdline.Append("reboot", "k")
cmdline.Append("panic", "1")
cmdline.Append("talos.platform", "metal")
cmdline.Append("talos.config", fmt.Sprintf("http://%s:9091/configdata?uuid=", cluster.SideroComponentsIP()))
environment.APIVersion = "metal.sidero.dev/v1alpha1"
environment.Name = environmentName
environment.Spec.Kernel.URL = kernelURL
environment.Spec.Kernel.SHA512 = "" // TODO: add a test
environment.Spec.Kernel.Args = cmdline.Strings()
environment.Spec.Initrd.URL = initrdURL
environment.Spec.Initrd.SHA512 = "" // TODO: add a test
require.NoError(t, metalClient.Create(ctx, &environment))
}
// wait for the environment to report ready
require.NoError(t, retry.Constant(5*time.Minute, retry.WithUnits(10*time.Second)).Retry(func() error {
if err := metalClient.Get(ctx, types.NamespacedName{Name: environmentName}, &environment); err != nil {
return retry.UnexpectedError(err)
}
assetURLs := map[string]struct{}{
kernelURL: {},
initrdURL: {},
}
for _, cond := range environment.Status.Conditions {
if cond.Status == "True" && cond.Type == "Ready" {
delete(assetURLs, cond.URL)
}
}
if len(assetURLs) > 0 {
return retry.ExpectedError(fmt.Errorf("some assets are not ready: %v", assetURLs))
}
return nil
}))
}
}