forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Third part of cockroachdb#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
Showing
3 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |