66package scenario_tests
77
88import (
9- "encoding/json"
10- "os"
11- "path/filepath"
12- "strings"
139 "testing"
1410
15- "github.com/gim-home/azldev-preview/scenario/internal/cmdtest "
11+ "github.com/gim-home/azldev-preview/scenario/internal/projecttest "
1612 "github.com/stretchr/testify/assert"
1713 "github.com/stretchr/testify/require"
1814)
@@ -26,61 +22,35 @@ func TestQueryingAComponent(t *testing.T) {
2622 t .Skip ("skipping long test" )
2723 }
2824
29- // Write out a simple script that writes a spec file and tries to query it .
30- testScript := `
31- set -euxo pipefail
32- azldev project init
33- ln -s /var/lib/mock build # Ensure the build output dir is writable by mock
25+ // Create a simple spec with a known name and version .
26+ spec := projecttest . NewSpec (
27+ projecttest . WithName ( "test-component" ),
28+ projecttest . WithVersion ( "3.1.4.159" ),
29+ )
3430
35- cat <<EOF >test-component.spec
36- Name: test-component
37- Version: 3.1.4.159
38- Release: 1%{?dist}
39- Summary: A test component
40- License: MIT
31+ // Create a simple project with the spec, using test default configs for distro and mock configurations.
32+ project := projecttest .NewDynamicTestProject (
33+ projecttest .AddSpec (spec ),
34+ projecttest .UseTestDefaultConfigs (),
35+ )
4136
42- %description
43- Test component for, you know, testing.
44- EOF
37+ // Run the component query command with test default configs copied into the container.
38+ results := projecttest .NewProjectTest (
39+ project ,
40+ []string {"component" , "query" , spec .GetName ()},
41+ projecttest .WithTestDefaultConfigs (),
42+ ).RunInContainer (t )
4543
46- ls -l -R
47-
48- azldev component list --all-components
49-
50- azldev -v component query test-component --output-format json >query-result.json
51- `
52-
53- // NOTE: We need to run in a privileged container so 'mock' can create its nested root environment.
54- // NOTE: We need to enable networking so 'mock' can download Azure Linux packages to build a root.
55- results , err := cmdtest .NewScenarioTest ().
56- WithScript (strings .NewReader (testScript )).
57- InContainer ().
58- WithPrivilege ().
59- WithNetwork ().
60- Run (t )
61-
62- require .NoError (t , err )
63- results .AssertZeroExitCode (t )
64-
65- // Find the output file.
66- outputFilePath := filepath .Join (results .Workdir , "query-result.json" )
67- require .FileExists (t , outputFilePath , "Expected output file to exist" )
68-
69- // Read it.
70- bytes , err := os .ReadFile (outputFilePath )
71- require .NoError (t , err , "Failed to read output file" )
72-
73- // Parse it as JSON
74- var output []map [string ]interface {}
75- require .NoError (t , json .Unmarshal (bytes , & output ))
44+ // Get the parsed JSON output.
45+ output := results .GetJSONResult ()
7646
7747 // There should just be one component in the output.
7848 require .Len (t , output , 1 , "Expected one component in the output" )
7949 componentOutput := output [0 ]
8050
8151 // Check for name.
8252 require .Contains (t , componentOutput , "Name" )
83- assert .Equal (t , componentOutput ["Name" ], "test-component" , "Expected component name to match" )
53+ assert .Equal (t , spec . GetName (), componentOutput ["Name" ], "Expected component name to match" )
8454
8555 // Check for EVR structure.
8656 require .Contains (t , componentOutput , "Version" )
@@ -90,5 +60,5 @@ azldev -v component query test-component --output-format json >query-result.json
9060 versionMap , ok := version .(map [string ]interface {})
9161 require .True (t , ok , "Version field is not a map" )
9262 require .Contains (t , versionMap , "Version" )
93- assert .Equal (t , versionMap ["Version" ], "3.1.4.159" )
63+ assert .Equal (t , spec . GetVersion (), versionMap ["Version" ])
9464}
0 commit comments