Count `coder_script_order` declarations and literal `rule` blocks in coderd from the existing START plan in `CompletedJob.TemplateImport.Plan`, including child modules, without multiplying counts by resource or module instances. Persist these counts per template version on successful import after START plan graph validation, without changing the provisioner API. Report a deployment-wide `script_order_summary` with the number of templates using script ordering and total declaration and rule counts. Include only active, successfully imported versions of non-deleted templates so repeated imports do not inflate adoption. Elect one reporting replica per snapshot period and report explicit zeros when no templates use ordering. The summary contains only aggregate counts, without template identifiers or rule contents. Dynamic rule blocks are not counted because Terraform omits them from configuration JSON. Configuration-decoding failures are logged and skip updating the counts without independently failing the import. Script-order summary collection failures are logged and omit that summary while allowing the rest of the snapshot to proceed. Refs: https://linear.app/codercom/issue/PLAT-551
127 lines
3.8 KiB
Go
127 lines
3.8 KiB
Go
package provisionerdserver
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
tfjson "github.com/hashicorp/terraform-json"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCountScriptOrderUsage(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, tt := range []struct {
|
|
name string
|
|
plan string
|
|
wantErr bool
|
|
}{
|
|
{name: "MissingPlan"},
|
|
{name: "NullPlan", plan: "null"},
|
|
{name: "MissingConfiguration", plan: "{}"},
|
|
{name: "EmptyConfiguration", plan: `{"configuration":{}}`},
|
|
{name: "InvalidJSON", plan: "{", wantErr: true},
|
|
{name: "InvalidConfiguration", plan: `{"configuration":[]}`, wantErr: true},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
usage, err := countScriptOrderUsage([]byte(tt.plan))
|
|
if tt.wantErr {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
require.Empty(t, usage)
|
|
})
|
|
}
|
|
|
|
checkUsage := func(t *testing.T, config *tfjson.Config, want scriptOrderUsage) {
|
|
t.Helper()
|
|
// The provisioner marshals a decoded plan before forwarding it to coderd.
|
|
planJSON, err := json.Marshal(&tfjson.Plan{Config: config})
|
|
require.NoError(t, err)
|
|
usage, err := countScriptOrderUsage(planJSON)
|
|
require.NoError(t, err)
|
|
require.Equal(t, want, usage)
|
|
}
|
|
|
|
declaration := func(ruleCount int) *tfjson.ConfigResource {
|
|
return &tfjson.ConfigResource{
|
|
Mode: tfjson.DataResourceMode,
|
|
Type: "coder_script_order",
|
|
Expressions: map[string]*tfjson.Expression{
|
|
"rule": {ExpressionData: &tfjson.ExpressionData{
|
|
NestedBlocks: make([]map[string]*tfjson.Expression, ruleCount),
|
|
}},
|
|
},
|
|
}
|
|
}
|
|
|
|
t.Run("ConfigurationDeclarationsNotInstances", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Three declarations (root, child, nested) contain 2 + 3 + 1 rules.
|
|
// Resource and module repetition must not multiply either total.
|
|
rootOrder := declaration(2)
|
|
rootOrder.ForEachExpression = &tfjson.Expression{ExpressionData: &tfjson.ExpressionData{
|
|
ConstantValue: map[string]string{"first": "", "second": ""},
|
|
}}
|
|
childOrder := declaration(3)
|
|
childOrder.CountExpression = &tfjson.Expression{ExpressionData: &tfjson.ExpressionData{
|
|
ConstantValue: 3, // Three resource instances per module instance.
|
|
}}
|
|
config := &tfjson.Config{RootModule: &tfjson.ConfigModule{
|
|
Resources: []*tfjson.ConfigResource{
|
|
rootOrder,
|
|
nil,
|
|
{Mode: tfjson.ManagedResourceMode, Type: "coder_script_order"},
|
|
{Mode: tfjson.DataResourceMode, Type: "coder_parameter"},
|
|
},
|
|
ModuleCalls: map[string]*tfjson.ModuleCall{
|
|
"repeated": {
|
|
ForEachExpression: &tfjson.Expression{ExpressionData: &tfjson.ExpressionData{
|
|
ConstantValue: map[string]string{"east": "", "west": ""},
|
|
}},
|
|
Module: &tfjson.ConfigModule{
|
|
Resources: []*tfjson.ConfigResource{childOrder},
|
|
ModuleCalls: map[string]*tfjson.ModuleCall{
|
|
"nested": {
|
|
CountExpression: &tfjson.Expression{ExpressionData: &tfjson.ExpressionData{
|
|
ConstantValue: 2,
|
|
}},
|
|
Module: &tfjson.ConfigModule{
|
|
Resources: []*tfjson.ConfigResource{declaration(1)},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
"nil": nil,
|
|
"empty": {},
|
|
},
|
|
}}
|
|
checkUsage(t, config, scriptOrderUsage{dataSourceCount: 3, ruleCount: 6})
|
|
})
|
|
|
|
t.Run("DeclarationWithZeroInstances", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
order := declaration(2)
|
|
order.CountExpression = &tfjson.Expression{ExpressionData: &tfjson.ExpressionData{ConstantValue: 0}}
|
|
config := &tfjson.Config{RootModule: &tfjson.ConfigModule{
|
|
Resources: []*tfjson.ConfigResource{order},
|
|
}}
|
|
checkUsage(t, config, scriptOrderUsage{dataSourceCount: 1, ruleCount: 2})
|
|
})
|
|
|
|
t.Run("NoLiteralRules", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
config := &tfjson.Config{RootModule: &tfjson.ConfigModule{
|
|
Resources: []*tfjson.ConfigResource{declaration(0)},
|
|
}}
|
|
checkUsage(t, config, scriptOrderUsage{dataSourceCount: 1})
|
|
})
|
|
}
|