-
Notifications
You must be signed in to change notification settings - Fork 9
/
tests.go
95 lines (76 loc) · 2.73 KB
/
tests.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cuefrontend
import (
"context"
"fmt"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/cuefrontend/args"
"namespacelabs.dev/foundation/internal/frontend/cuefrontend/binary"
integrationparsing "namespacelabs.dev/foundation/internal/frontend/cuefrontend/integration/api"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
// Needs to be consistent with JSON names of cueSecret fields.
var testFields = []string{"serversUnderTest", "args", "env", "image", "imageFrom", "integration"}
type cueTest struct {
Servers []string `json:"serversUnderTest"`
Args *args.ArgsListOrMap `json:"args"`
Env *args.EnvMap `json:"env"`
}
// New syntax
func parseTests(ctx context.Context, env *schema.Environment, pl parsing.EarlyPackageLoader, pkg *pkggraph.Package, v *fncue.CueV) ([]*schema.Test, error) {
it, err := v.Val.Fields()
if err != nil {
return nil, err
}
out := []*schema.Test{}
for it.Next() {
parsedTest, err := parseTest(ctx, env, pl, pkg, it.Label(), (&fncue.CueV{Val: it.Value()}))
if err != nil {
return nil, err
}
out = append(out, parsedTest)
}
return out, nil
}
func parseTest(ctx context.Context, env *schema.Environment, pl parsing.EarlyPackageLoader, pkg *pkggraph.Package, name string, v *fncue.CueV) (*schema.Test, error) {
if err := ValidateNoExtraFields(pkg.Location, fmt.Sprintf("test %q:", name) /* messagePrefix */, v, testFields); err != nil {
return nil, err
}
var bits cueTest
if err := v.Val.Decode(&bits); err != nil {
return nil, err
}
envVars, err := bits.Env.Parsed(ctx, pl, pkg.Location)
if err != nil {
return nil, err
}
out := &schema.Test{
Name: name,
ServersUnderTest: bits.Servers,
BinaryConfig: &schema.BinaryConfig{
Args: bits.Args.Parsed(),
Env: envVars,
},
}
binaryRef, err := binary.ParseImage(ctx, env, pl, pkg, name, v, binary.ParseImageOpts{Required: false})
if err != nil {
return nil, fnerrors.NewWithLocation(pkg.Location, "parsing test %q failed: %w", name, err)
}
out.Driver = binaryRef
if i := v.LookupPath("integration"); i.Exists() {
integration, err := integrationparsing.IntegrationParser.ParseEntity(ctx, env, pl, pkg.Location, i)
if err != nil {
return nil, err
}
out.Integration = &schema.Integration{
Data: protos.WrapAnyOrDie(integration.Data),
}
}
return out, nil
}