Skip to content

Commit

Permalink
Restore json format for fields as well as whole structs
Browse files Browse the repository at this point in the history
* Add template func to inspect template processing
* Added test using repro from containers#8444

Fixes containers#8444

Signed-off-by: Jhon Honce <[email protected]>
  • Loading branch information
jwhonce committed Dec 7, 2020
1 parent e2f9120 commit ce47478
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cmd/podman/inspect/inspect.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspect

import (
"bytes"
"context"
"encoding/json" // due to a bug in json-iterator it cannot be used here
"fmt"
Expand Down Expand Up @@ -245,7 +246,15 @@ func printJSON(data []interface{}) error {
}

func printTmpl(typ, row string, data []interface{}) error {
t, err := template.New(typ + " inspect").Parse(row)
t, err := template.New(typ + " inspect").Funcs(map[string]interface{}{
"json": func(v interface{}) string {
b := &bytes.Buffer{}
e := registry.JSONLibrary().NewEncoder(b)
e.SetEscapeHTML(false)
_ = e.Encode(v)
return strings.TrimSpace(b.String())
},
}).Parse(row)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
. "github.com/containers/podman/v2/test/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
"github.com/opencontainers/selinux/go-selinux"
)

Expand Down Expand Up @@ -428,4 +429,18 @@ var _ = Describe("Podman inspect", func() {
Expect(inspect).To(ExitWithError())
})

// Fixes https://github.com/containers/podman/issues/8444
It("podman inspect --format json .NetworkSettings.Ports", func() {
ctnrName := "Ctnr_" + RandomString(25)

create := podmanTest.Podman([]string{"create", "--name", ctnrName, "-p", "8080:80", ALPINE})
create.WaitWithDefaultTimeout()
Expect(create).Should(Exit(0))

inspect := podmanTest.Podman([]string{"inspect", `--format="{{json .NetworkSettings.Ports}}"`, ctnrName})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(Exit(0))
Expect(inspect.OutputToString()).To(Equal(`"{"80/tcp":[{"HostIp":"","HostPort":"8080"}]}"`))
})

})
14 changes: 14 additions & 0 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"runtime"
Expand Down Expand Up @@ -465,3 +466,16 @@ func Containerized() bool {
}
return false
}

var randomLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

// RandomString returns a string of given length composed of random characters
func RandomString(n int) string {
rand.Seed(GinkgoRandomSeed())

b := make([]rune, n)
for i := range b {
b[i] = randomLetters[rand.Intn(len(randomLetters))]
}
return string(b)
}

0 comments on commit ce47478

Please sign in to comment.