Skip to content

Commit

Permalink
test connection add
Browse files Browse the repository at this point in the history
* Fix connection JSON encoding
* Add custom ginkgo matchers for connection testing
* Cleanup code

Fixes #11984

Signed-off-by: Jhon Honce <[email protected]>
  • Loading branch information
jwhonce committed Nov 8, 2021
1 parent 22ef488 commit e907f09
Show file tree
Hide file tree
Showing 10 changed files with 451 additions and 299 deletions.
12 changes: 4 additions & 8 deletions cmd/podman/images/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func loadToRemote(localFile string, tag string, url *urlP.URL, iden string) (str

n, err := scpD.CopyTo(dial, localFile, remoteFile)
if err != nil {
errOut := (strconv.Itoa(int(n)) + " Bytes copied before error")
errOut := strconv.Itoa(int(n)) + " Bytes copied before error"
return " ", errors.Wrapf(err, errOut)
}
run := ""
Expand All @@ -181,7 +181,7 @@ func loadToRemote(localFile string, tag string, url *urlP.URL, iden string) (str
if err != nil {
return "", err
}
return strings.TrimSuffix(out, "\n"), nil
return strings.TrimSuffix(string(out), "\n"), nil
}

// saveToRemote takes image information and remote connection information. it connects to the specified client
Expand All @@ -207,7 +207,7 @@ func saveToRemote(image, localFile string, tag string, uri *urlP.URL, iden strin
n, err := scpD.CopyFrom(dial, remoteFile, localFile)
connection.ExecRemoteCommand(dial, "rm "+remoteFile)
if err != nil {
errOut := (strconv.Itoa(int(n)) + " Bytes copied before error")
errOut := strconv.Itoa(int(n)) + " Bytes copied before error"
return errors.Wrapf(err, errOut)
}
return nil
Expand All @@ -221,11 +221,7 @@ func makeRemoteFile(dial *ssh.Client) (string, error) {
if err != nil {
return "", err
}
remoteFile = strings.TrimSuffix(remoteFile, "\n")
if err != nil {
return "", err
}
return remoteFile, nil
return strings.TrimSuffix(string(remoteFile), "\n"), nil
}

// createConnections takes a boolean determining which ssh client to dial
Expand Down
7 changes: 1 addition & 6 deletions cmd/podman/system/connection/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,7 @@ func getUDS(cmd *cobra.Command, uri *url.URL, iden string) (string, error) {
if v, found := os.LookupEnv("PODMAN_BINARY"); found {
podman = v
}
run := podman + " info --format=json"
out, err := ExecRemoteCommand(dial, run)
if err != nil {
return "", err
}
infoJSON, err := json.Marshal(out)
infoJSON, err := ExecRemoteCommand(dial, podman+" info --format=json")
if err != nil {
return "", err
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/podman/system/connection/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

// ExecRemoteCommand takes a ssh client connection and a command to run and executes the
// command on the specified client. The function returns the Stdout from the client or the Stderr
func ExecRemoteCommand(dial *ssh.Client, run string) (string, error) {
func ExecRemoteCommand(dial *ssh.Client, run string) ([]byte, error) {
sess, err := dial.NewSession() // new ssh client session
if err != nil {
return "", err
return nil, err
}
defer sess.Close()

Expand All @@ -21,8 +21,7 @@ func ExecRemoteCommand(dial *ssh.Client, run string) (string, error) {
sess.Stdout = &buffer // output from client funneled into buffer
sess.Stderr = &bufferErr // err form client funneled into buffer
if err := sess.Run(run); err != nil { // run the command on the ssh client
return "", errors.Wrapf(err, bufferErr.String())
return nil, errors.Wrapf(err, bufferErr.String())
}
out := buffer.String() // output from command
return out, nil
return buffer.Bytes(), nil
}
50 changes: 31 additions & 19 deletions contrib/cirrus/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -166,30 +166,42 @@ setup_rootless() {
useradd -g $rootless_gid -u $rootless_uid --no-user-group --create-home $ROOTLESS_USER
chown -R $ROOTLESS_USER:$ROOTLESS_USER "$GOPATH" "$GOSRC"

msg "creating ssh key pair for $USER"
mkdir -p "$HOME/.ssh" "/home/$ROOTLESS_USER/.ssh"

msg "Creating ssh key pairs"
[[ -r "$HOME/.ssh/id_rsa" ]] || \
ssh-keygen -P "" -f "$HOME/.ssh/id_rsa"
ssh-keygen -t rsa -P "" -f "$HOME/.ssh/id_rsa"
ssh-keygen -t ed25519 -P "" -f "/home/$ROOTLESS_USER/.ssh/id_ed25519"
ssh-keygen -t rsa -P "" -f "/home/$ROOTLESS_USER/.ssh/id_rsa"

msg "Allowing ssh key for $ROOTLESS_USER"
akfilepath="/home/$ROOTLESS_USER/.ssh/authorized_keys"
(umask 077 && mkdir "/home/$ROOTLESS_USER/.ssh")
chown -R $ROOTLESS_USER:$ROOTLESS_USER "/home/$ROOTLESS_USER/.ssh"
install -o $ROOTLESS_USER -g $ROOTLESS_USER -m 0600 \
"$HOME/.ssh/id_rsa.pub" "$akfilepath"
# Makes debugging easier
cat /root/.ssh/authorized_keys >> "$akfilepath"
msg "Setup authorized_keys"
cat $HOME/.ssh/*.pub /home/$ROOTLESS_USER/.ssh/*.pub >> $HOME/.ssh/authorized_keys
cat $HOME/.ssh/*.pub /home/$ROOTLESS_USER/.ssh/*.pub >> /home/$ROOTLESS_USER/.ssh/authorized_keys

msg "Ensure the ssh daemon is up and running within 5 minutes"
systemctl start sshd
sshcmd="ssh $ROOTLESS_USER@localhost
-o UserKnownHostsFile=/dev/null
-o StrictHostKeyChecking=no
-o CheckHostIP=no"
lilto $sshcmd true # retry until sshd is up

msg "Configuring rootless user self-access to ssh to localhost"
$sshcmd ssh-keygen -P '""' -f "/home/$ROOTLESS_USER/.ssh/id_rsa"
cat "/home/$ROOTLESS_USER/.ssh/id_rsa" >> "$akfilepath"
lilto systemctl is-active sshd

msg "Configure ssh file permissions"
chmod -R 700 "$HOME/.ssh"
chmod -R 700 "/home/$ROOTLESS_USER/.ssh"
chown -R $ROOTLESS_USER:$ROOTLESS_USER "/home/$ROOTLESS_USER/.ssh"

msg " setup known_hosts for $USER"
ssh -q root@localhost \
-o UserKnownHostsFile=/root/.ssh/known_hosts \
-o UpdateHostKeys=yes \
-o StrictHostKeyChecking=no \
-o CheckHostIP=no \
true

msg " setup known_hosts for $ROOTLESS_USER"
su $ROOTLESS_USER -c "ssh -q $ROOTLESS_USER@localhost \
-o UserKnownHostsFile=/home/$ROOTLESS_USER/.ssh/known_hosts \
-o UpdateHostKeys=yes \
-o StrictHostKeyChecking=no \
-o CheckHostIP=no \
true"
}

install_test_configs() {
Expand Down
76 changes: 61 additions & 15 deletions test/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ var _ = SynchronizedAfterSuite(func() {},

// PodmanTestCreate creates a PodmanTestIntegration instance for the tests
func PodmanTestCreateUtil(tempDir string, remote bool) *PodmanTestIntegration {
var (
podmanRemoteBinary string
)
var podmanRemoteBinary string

host := GetHostDistributionInfo()
cwd, _ := os.Getwd()
Expand All @@ -220,12 +218,11 @@ func PodmanTestCreateUtil(tempDir string, remote bool) *PodmanTestIntegration {
podmanBinary = os.Getenv("PODMAN_BINARY")
}

if remote {
podmanRemoteBinary = filepath.Join(cwd, "../../bin/podman-remote")
if os.Getenv("PODMAN_REMOTE_BINARY") != "" {
podmanRemoteBinary = os.Getenv("PODMAN_REMOTE_BINARY")
}
podmanRemoteBinary = filepath.Join(cwd, "../../bin/podman-remote")
if os.Getenv("PODMAN_REMOTE_BINARY") != "" {
podmanRemoteBinary = os.Getenv("PODMAN_REMOTE_BINARY")
}

conmonBinary := filepath.Join("/usr/libexec/podman/conmon")
altConmonBinary := "/usr/bin/conmon"
if _, err := os.Stat(conmonBinary); os.IsNotExist(err) {
Expand Down Expand Up @@ -271,12 +268,13 @@ func PodmanTestCreateUtil(tempDir string, remote bool) *PodmanTestIntegration {

p := &PodmanTestIntegration{
PodmanTest: PodmanTest{
PodmanBinary: podmanBinary,
ArtifactPath: ARTIFACT_DIR,
TempDir: tempDir,
RemoteTest: remote,
ImageCacheFS: storageFs,
ImageCacheDir: ImageCacheDir,
PodmanBinary: podmanBinary,
RemotePodmanBinary: podmanRemoteBinary,
ArtifactPath: ARTIFACT_DIR,
TempDir: tempDir,
RemoteTest: remote,
ImageCacheFS: storageFs,
ImageCacheDir: ImageCacheDir,
},
ConmonBinary: conmonBinary,
CrioRoot: filepath.Join(tempDir, "crio"),
Expand All @@ -289,8 +287,8 @@ func PodmanTestCreateUtil(tempDir string, remote bool) *PodmanTestIntegration {
CgroupManager: cgroupManager,
Host: host,
}

if remote {
p.PodmanTest.RemotePodmanBinary = podmanRemoteBinary
uuid := stringid.GenerateNonCryptoID()
if !rootless.IsRootless() {
p.RemoteSocket = fmt.Sprintf("unix:/run/podman/podman-%s.sock", uuid)
Expand Down Expand Up @@ -632,6 +630,19 @@ func SkipIfNotRootless(reason string) {
}
}

func SkipIfSystemdNotRunning(reason string) {
checkReason(reason)

cmd := exec.Command("systemctl", "list-units")
err := cmd.Run()
if err != nil {
if _, ok := err.(*exec.Error); ok {
ginkgo.Skip("[notSystemd]: not running " + reason)
}
Expect(err).ToNot(HaveOccurred())
}
}

func SkipIfNotSystemd(manager, reason string) {
checkReason(reason)
if manager != "systemd" {
Expand Down Expand Up @@ -683,6 +694,41 @@ func SkipIfContainerized(reason string) {
}
}

func SkipIfRemote(reason string) {
checkReason(reason)
if !IsRemote() {
return
}
ginkgo.Skip("[remote]: " + reason)
}

// SkipIfInContainer skips a test if the test is run inside a container
func SkipIfInContainer(reason string) {
checkReason(reason)
if os.Getenv("TEST_ENVIRON") == "container" {
Skip("[container]: " + reason)
}
}

// SkipIfNotActive skips a test if the given systemd unit is not active
func SkipIfNotActive(unit string, reason string) {
checkReason(reason)

var buffer bytes.Buffer
cmd := exec.Command("systemctl", "is-active", unit)
cmd.Stdout = &buffer
err := cmd.Start()
Expect(err).ToNot(HaveOccurred())

err = cmd.Wait()
Expect(err).ToNot(HaveOccurred())

Expect(err).ToNot(HaveOccurred())
if strings.TrimSpace(buffer.String()) != "active" {
Skip(fmt.Sprintf("[systemd]: unit %s is not active: %s", unit, reason))
}
}

// PodmanAsUser is the exec call to podman on the filesystem with the specified uid/gid and environment
func (p *PodmanTestIntegration) PodmanAsUser(args []string, uid, gid uint32, cwd string, env []string) *PodmanSessionIntegration {
podmanSession := p.PodmanAsUserBase(args, uid, gid, cwd, env, false, false, nil, nil)
Expand Down
8 changes: 0 additions & 8 deletions test/e2e/libpod_suite_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,12 @@ import (
"time"

"github.com/containers/podman/v3/pkg/rootless"
"github.com/onsi/ginkgo"
)

func IsRemote() bool {
return true
}

func SkipIfRemote(reason string) {
if len(reason) < 5 {
panic("SkipIfRemote must specify a reason to skip")
}
ginkgo.Skip("[remote]: " + reason)
}

// Podman is the exec call to podman on the filesystem
func (p *PodmanTestIntegration) Podman(args []string) *PodmanSessionIntegration {
var remoteArgs = []string{"--remote", "--url", p.RemoteSocket}
Expand Down
3 changes: 0 additions & 3 deletions test/e2e/libpod_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ func IsRemote() bool {
return false
}

func SkipIfRemote(string) {
}

// Podman is the exec call to podman on the filesystem
func (p *PodmanTestIntegration) Podman(args []string) *PodmanSessionIntegration {
podmanSession := p.PodmanBase(args, false, false)
Expand Down
Loading

0 comments on commit e907f09

Please sign in to comment.