Skip to content

Commit

Permalink
roachprod: refactor ssh.go
Browse files Browse the repository at this point in the history
Third part of #47567 where we need to refactor ssh.go (as one type structure is in use, we cannot remove it completely).

So we are refactoring ssh.go and change its name to io.go and only keeping ProgressWriter struct and function

Also we are adding a unit test go code to ensure it works as expected.

Release note: None
  • Loading branch information
alan-mas committed Jul 28, 2021
1 parent 1159281 commit c0c0868
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/cmd/roachprod/ssh/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "ssh",
srcs = [
"io.go",
"shell.go",
"ssh.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/cmd/roachprod/ssh",
visibility = ["//visibility:public"],
)

go_test(
name = "ssh_test",
srcs = ["io_test.go"],
embed = [":ssh"],
)
File renamed without changes.
49 changes: 49 additions & 0 deletions pkg/cmd/roachprod/ssh/io_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package ssh

import (
"io/ioutil"
"testing"
)

func TestProgress(t *testing.T) {
output, err := ioutil.TempFile("", "example*")
if err != nil {
t.Fatal(err)
}
defer output.Close()

b := make([]byte, 10)
var percent float64
writer := &ProgressWriter{
Writer: output,
Done: 0,
Total: 50,
Progress: func(currentProgress float64) {
percent = currentProgress
},
}
for i := 0; i < 4; i++ {
if _, err := writer.Write(b); err != nil {
t.Fatal(err)
}
}
if percent != 0.8 {
t.Errorf("expected progress of 80%% but got %.2f", percent*100)
}
if _, err := writer.Write(b); err != nil {
t.Fatal(err)
}
if percent != 1.0 {
t.Errorf("expected progress of 100%% but got %.2f", percent*100)
}
}

0 comments on commit c0c0868

Please sign in to comment.