Skip to content

Commit 94f53bc

Browse files
committed
test: add resource download test and charm
1 parent 4ea69c0 commit 94f53bc

File tree

11 files changed

+92
-11
lines changed

11 files changed

+92
-11
lines changed

cmd/juju/charmhub/download.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ type downloadCommand struct {
7373
pipeToStdout bool
7474
noProgress bool
7575
resources bool
76-
noResources bool
7776
}
7877

7978
// Info returns help related download about the command, it implements
@@ -106,7 +105,6 @@ func (c *downloadCommand) SetFlags(f *gnuflag.FlagSet) {
106105
f.StringVar(&c.archivePath, "filepath", "", "filepath location of the charm to download to")
107106
f.BoolVar(&c.noProgress, "no-progress", false, "disable the progress bar")
108107
f.BoolVar(&c.resources, "resources", false, "download the resources associated with the charm (will be DEPRECATED and default behaviour in 4.0)")
109-
f.BoolVar(&c.noResources, "no-resources", false, "disable downloading the resources associated with the charm")
110108
}
111109

112110
// Init initializes the download command, including validating the provided
@@ -138,8 +136,8 @@ func (c *downloadCommand) Init(args []string) error {
138136
c.pipeToStdout = true
139137
}
140138

141-
if c.pipeToStdout && c.resources && !c.noResources {
142-
return errors.Errorf("cannot pipe to stdout and download resources: pass --no-resources to download to stdout")
139+
if c.pipeToStdout && c.resources {
140+
return errors.Errorf("cannot pipe to stdout and download resources: do not pass --resources to download to stdout")
143141
}
144142

145143
curl, err := c.validateCharmOrBundle(args[0])
@@ -288,7 +286,7 @@ Calculated: %s`, c.charmOrBundle, entitySHA, calculatedHash)
288286
}
289287

290288
rscPaths := make(map[string]string)
291-
if c.resources && !c.noResources {
289+
if c.resources {
292290
dir := filepath.Dir(path)
293291

294292
for _, resource := range entity.Resources {
@@ -327,7 +325,7 @@ Calculated: %s`, c.charmOrBundle, resource.Name, resource.Download.HashSHA256, r
327325
path = fmt.Sprintf("./%s", path)
328326
}
329327

330-
if c.resources && !c.noResources && len(entity.Resources) > 0 {
328+
if c.resources && len(entity.Resources) > 0 {
331329
resourceArgs := []string{}
332330
for _, resource := range entity.Resources {
333331
rscPath := rscPaths[resource.Name]

cmd/juju/charmhub/download_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (s *downloadSuite) TestRunWithInvalidStdout(c *gc.C) {
220220
c.Assert(err, gc.ErrorMatches, `expected a charm or bundle name, followed by hyphen to pipe to stdout`)
221221

222222
err = cmdtesting.InitCommand(command, []string{"test", "--resources", "-"})
223-
c.Check(err, gc.ErrorMatches, `cannot pipe to stdout and download resources: pass --no-resources to download to stdout`)
223+
c.Check(err, gc.ErrorMatches, `cannot pipe to stdout and download resources: do not pass --resources to download to stdout`)
224224
}
225225

226226
func (s *downloadSuite) TestRunWithRevision(c *gc.C) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: juju-qa-test-resources
2+
type: charm
3+
title: Juju QA Test resources charm
4+
summary: A testing charm used to validate resources.
5+
description: |
6+
A testing charm used to validate resources in the Juju QA test infra.
7+
bases:
8+
- build-on:
9+
- name: ubuntu
10+
channel: "22.04"
11+
run-on:
12+
- name: ubuntu
13+
channel: "22.04"
14+
resources:
15+
runnable:
16+
type: file
17+
filename: runnable
18+
description: an executable
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ops ~= 2.5
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
default:
2+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ../runnable_amd64.out main.go
3+
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o ../runnable_arm64.out main.go
4+
5+
.PHONY: default
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2022 Canonical Ltd.
2+
// Licensed under the AGPLv3, see LICENCE file for details.
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"runtime"
9+
)
10+
11+
func main() {
12+
fmt.Printf("%s-%s", runtime.GOARCH, runtime.GOOS)
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
3+
import logging
4+
import os
5+
import stat
6+
import subprocess
7+
8+
import ops
9+
10+
# Log messages can be retrieved using juju debug-log
11+
logger = logging.getLogger(__name__)
12+
13+
class JujuQaTestResourcesCharm(ops.CharmBase):
14+
"""Charm the service."""
15+
16+
def __init__(self, *args):
17+
super().__init__(*args)
18+
self.framework.observe(self.on.install, self._on_install)
19+
20+
def _on_install(self, event: ops.InstallEvent):
21+
resource_path = self.model.resources.fetch("runnable")
22+
os.chmod(resource_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
23+
res = subprocess.run(resource_path, capture_output=True, text=True)
24+
logger.info("runnable=%s", res.stdout)
25+
self.unit.status = ops.ActiveStatus(res.stdout)
26+
27+
if __name__ == "__main__": # pragma: nocover
28+
ops.main(JujuQaTestResourcesCharm) # type: ignore

tests/suites/charmhub/download.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ run_charmhub_download() {
1515
destroy_model "${name}"
1616
}
1717

18+
run_charmhub_download_with_resources() {
19+
echo
20+
name="charmhub-download-with-resources"
21+
22+
file="${TEST_DIR}/${name}.log"
23+
24+
ensure "${name}" "${file}"
25+
26+
output=$(juju download juju-qa-test-resources --resources --filepath="${TEST_DIR}/juju-qa-test-resources.charm" 2>&1 || true)
27+
check_contains "${output}" 'Fetching charm "juju-qa-test-resources"'
28+
29+
$(echo "${output}" | grep "juju deploy") juju-qa-test-resources
30+
wait_for "${MODEL_ARCH:-amd64}-linux" "$(workload_status juju-qa-test-resources 0).message"
31+
32+
destroy_model "${name}"
33+
}
34+
1835
run_charmstore_download() {
1936
echo
2037
name="test-charmstore-download"
@@ -45,7 +62,7 @@ run_unknown_download() {
4562

4663
test_charmhub_download() {
4764
if [ "$(skip 'test_charmhub_download')" ]; then
48-
echo "==> TEST SKIPPED: Charmhub download"
65+
echo "==> TEST SKIPPED: charmhub download"
4966
return
5067
fi
5168

@@ -55,6 +72,7 @@ test_charmhub_download() {
5572
cd .. || exit
5673

5774
run "run_charmhub_download"
75+
run "run_charmhub_download_with_resources"
5876
run "run_charmstore_download"
5977
run "run_unknown_download"
6078
)

tests/suites/charmhub/find.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ run_charmstore_find() {
3434

3535
test_charmhub_find() {
3636
if [ "$(skip 'test_charmhub_find')" ]; then
37-
echo "==> TEST SKIPPED: Charm Hub find"
37+
echo "==> TEST SKIPPED: charmhub find"
3838
return
3939
fi
4040

tests/suites/charmhub/info.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ run_charmstore_info() {
4848

4949
test_charmhub_info() {
5050
if [ "$(skip 'test_charmhub_info')" ]; then
51-
echo "==> TEST SKIPPED: Charm Hub info"
51+
echo "==> TEST SKIPPED: charmhub info"
5252
return
5353
fi
5454

tests/suites/charmhub/task.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test_charmhub() {
22
if [ "$(skip 'test_charmhub')" ]; then
3-
echo "==> TEST SKIPPED: Charm Hub tests"
3+
echo "==> TEST SKIPPED: charmhub tests"
44
return
55
fi
66

0 commit comments

Comments
 (0)