buildkit/convert_test.go
buildkit/convert_test.goBrowse 1970 files
849 tokens
3,220 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package buildkit2 3import (4 "context"5 "path/filepath"6 "slices"7 "strconv"8 "strings"9 "testing"10 "time"11 12 specs "github.com/opencontainers/image-spec/specs-go/v1"13 "github.com/railwayapp/railpack/buildkit/build_llb"14 "github.com/railwayapp/railpack/core"15 "github.com/railwayapp/railpack/core/app"16 "github.com/railwayapp/railpack/core/plan"17 "github.com/stretchr/testify/require"18)19 20func TestGetImageEnvIncludesBuiltAt(t *testing.T) {21 graphOutput := &build_llb.BuildGraphOutput{22 GraphEnv: build_llb.NewGraphEnvironment(),23 }24 buildPlan := plan.NewBuildPlan()25 buildPlan.Deploy.Variables = map[string]string{26 "RAILPACK_BUILT_AT": "custom",27 }28 before := time.Now().Unix()29 30 env := getImageEnv(graphOutput, buildPlan)31 after := time.Now().Unix()32 33 builtAt := ""34 for _, variable := range env {35 if value, ok := strings.CutPrefix(variable, "RAILPACK_BUILT_AT="); ok {36 builtAt = value37 }38 }39 timestamp, err := strconv.ParseInt(builtAt, 10, 64)40 require.NoError(t, err)41 require.GreaterOrEqual(t, timestamp, before)42 require.LessOrEqual(t, timestamp, after)43 require.True(t, slices.IsSorted(env))44}45 46func TestImagePathHasNoDuplicateEntries(t *testing.T) {47 userApp, err := app.NewApp(filepath.Join("..", "examples", "ruby-with-node"))48 require.NoError(t, err)49 50 buildResult, err := core.GenerateBuildPlan(userApp, app.NewEnvironment(nil), &core.GenerateBuildPlanOptions{})51 require.NoError(t, err)52 require.True(t, buildResult.Success)53 54 _, image, err := ConvertPlanToLLB(buildResult.Plan, ConvertPlanOptions{55 BuildPlatform: specs.Platform{OS: "linux", Architecture: "amd64"},56 })57 require.NoError(t, err)58 59 path := pathFromEnv(t, image.Config.Env)60 require.Empty(t, duplicatePathEntries(path))61}62 63func pathFromEnv(t *testing.T, env []string) string {64 t.Helper()65 66 for _, envVar := range env {67 if value, ok := strings.CutPrefix(envVar, "PATH="); ok {68 return value69 }70 }71 t.Fatal("PATH not found in image environment")72 return ""73}74 75func duplicatePathEntries(path string) []string {76 seen := map[string]bool{}77 duplicates := []string{}78 for entry := range strings.SplitSeq(path, ":") {79 if seen[entry] {80 duplicates = append(duplicates, entry)81 }82 seen[entry] = true83 }84 return duplicates85}86 87func TestDeployVariablesDoNotAffectLLB(t *testing.T) {88 firstPlan := plan.NewBuildPlan()89 firstPlan.Deploy.Base = plan.NewImageLayer("alpine:latest")90 firstPlan.Deploy.Variables = map[string]string{"VALUE": "one"}91 secondPlan := plan.NewBuildPlan()92 secondPlan.Deploy.Base = plan.NewImageLayer("alpine:latest")93 secondPlan.Deploy.Variables = map[string]string{"VALUE": "two"}94 opts := ConvertPlanOptions{95 BuildPlatform: specs.Platform{96 OS: "linux",97 Architecture: "amd64",98 },99 }100 101 firstState, firstImage, err := ConvertPlanToLLB(firstPlan, opts)102 require.NoError(t, err)103 secondState, secondImage, err := ConvertPlanToLLB(secondPlan, opts)104 require.NoError(t, err)105 106 firstDefinition, err := firstState.Marshal(context.Background())107 require.NoError(t, err)108 secondDefinition, err := secondState.Marshal(context.Background())109 require.NoError(t, err)110 111 require.Equal(t, firstDefinition.ToPB(), secondDefinition.ToPB())112 require.NotEqual(t, firstImage.Config.Env, secondImage.Config.Env)113}114