Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

[インデックス 12449] ファイルの概要

このコミットは、Go言語の標準ライブラリ net パッケージ内のテストファイル群に対する変更です。具体的には、src/pkg/net/dial_test.gosrc/pkg/net/net_test.gosrc/pkg/net/server_test.gosrc/pkg/net/unicast_test.go の4つのファイルが修正されています。主な目的は、テスト実行時に外部ネットワークアドレスの使用を無効化し、特にmacOS (旧OS X) 環境で発生するファイアウォールダイアログのポップアップを回避することにあります。

コミット

commit 7c7966a4260b9f6c7c8e13586a04ec23f433670a
Author: Russ Cox <rsc@golang.org>
Date:   Tue Mar 6 23:35:31 2012 -0500

    net: disable use of external listen along with other external network uses
    
    By default the all.bash tests must not ever announce
    on an external address.  It's not just an OS X issue.
    
    R=golang-dev, mikioh.mikioh
    CC=golang-dev
    https://golang.org/cl/5753067

GitHub上でのコミットページへのリンク

https://github.com/golang/go/commit/7c7966a4260b9f6c7c8e13586a04ec23f433670a

元コミット内容

net: disable use of external listen along with other external network uses

By default the all.bash tests must not ever announce
on an external address.  It's not just an OS X issue.

R=golang-dev, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/5753067

変更の背景

このコミットの主な背景は、Go言語のテストスイート(特に all.bash スクリプトによって実行されるもの)が、外部ネットワークアドレス(例: 0.0.0.0[::] のようなワイルドカードアドレス)でリッスンしようとすると、macOS (旧OS X) 環境でファイアウォールダイアログが頻繁にポップアップするという問題にありました。

macOSのファイアウォールは、アプリケーションが外部からの接続を受け入れようとすると、セキュリティ上の理由からユーザーに許可を求めるダイアログを表示します。Goのテストスイートは、ネットワーク機能のテストのために一時的な実行可能ファイルを多数生成し、これらが外部アドレスでリッスンを試みるため、テストを実行するたびに繰り返しダイアログが表示され、開発者の作業を妨げていました。

コミットメッセージには「It's not just an OS X issue.」とあり、この問題がmacOSに限定されるものではなく、他のOSでも外部ネットワークアドレスの使用が問題を引き起こす可能性があることを示唆しています。そのため、デフォルトで all.bash テストが外部アドレスで通知(リッスン)しないようにする、というより一般的な解決策が求められました。

前提知識の解説

testing.Short()

Go言語の testing パッケージには testing.Short() という関数があります。これは、go test コマンドが -short フラグ付きで呼び出されたかどうかを判定するために使用されます。

  • 目的: 時間のかかるテストやリソースを大量に消費するテスト(例: 統合テスト、外部サービスに依存するテスト)を、開発中の迅速なフィードバックのためにスキップしたり、動作を変更したりするために利用されます。
  • 動作: go test -short で実行された場合、testing.Short()true を返します。それ以外の場合は false を返します。
  • 一般的な使用法:
    func TestTimeConsuming(t *testing.T) {
        if testing.Short() {
            t.Skip("skipping test in short mode.") // -short フラグがある場合はテストをスキップ
        }
        // ... 時間のかかるテストのロジック ...
    }
    
    これにより、開発者は -short フラグを使って高速なテスト実行を行い、CI/CD環境などではフルテストを実行するといった使い分けが可能になります。

外部ネットワーク使用と *testExternal フラグ

Goのテストにおいて、外部ネットワークリソース(インターネット上のサーバー、特定のIPアドレスなど)を使用するテストは、環境依存性や実行時間の問題を引き起こす可能性があります。このようなテストは、通常、特定の条件下でのみ実行されるべきです。

このコミットで参照されている *testExternal は、Goのテストフレームワーク内で定義された(またはテストコード内で定義された)フラグであると推測されます。これは、テストが外部ネットワークリソースを使用することを明示的に許可するためのコマンドライン引数(例: -testExternal)に対応するものです。

  • !*testExternal は、「testExternal フラグが設定されていない(つまり、外部ネットワークの使用が許可されていない)場合」を意味します。

このコミットの変更は、testing.Short()!*testExternal を組み合わせることで、デフォルトでは外部ネットワークを使用するテストをスキップし、明示的に -testExternal フラグが指定された場合にのみ実行されるように制御しています。

技術的詳細

このコミットの技術的な変更は、主に以下の2点に集約されます。

  1. avoidOSXFirewallDialogPopup 関数の削除: src/pkg/net/net_test.go から、macOSのファイアウォールダイアログを回避するための avoidOSXFirewallDialogPopup という変数が削除されました。この変数は、testing.Short()true でかつ runtime.GOOS"darwin" (macOS) の場合に true を返すように定義されていました。これは、macOSでのみ発生するファイアウォール問題を一時的に回避するための、OS固有のハックでした。

  2. テスト条件の変更: avoidOSXFirewallDialogPopup() の呼び出し箇所が、より汎用的な (testing.Short() || !*testExternal) という条件に置き換えられました。

    • testing.Short(): 前述の通り、-short フラグが指定された場合に true を返します。これにより、開発中の迅速なテスト実行時には、外部ネットワークを使用するテストがスキップされます。
    • !*testExternal: testExternal というフラグが設定されていない場合に true を返します。これは、デフォルトでは外部ネットワークの使用が許可されていないことを意味します。

この変更により、以下のいずれかの条件が満たされる場合に、外部ネットワークを使用する可能性のあるテストがスキップされるようになります。

  • go test -short が実行された場合。
  • testExternal フラグが明示的に設定されていない場合。

このアプローチは、macOS固有のファイアウォール問題だけでなく、all.bash テストがデフォルトで外部アドレスにリッスンしないようにするという、より広範な目標を達成します。これにより、テストの実行環境に依存しない、より堅牢で予測可能なテスト動作が実現されます。

また、src/pkg/net/dial_test.go では、コメント内のスペルミス unforunateunfortunate に修正されています。これは機能的な変更ではありませんが、コード品質の向上に貢献しています。

コアとなるコードの変更箇所

diff --git a/src/pkg/net/dial_test.go b/src/pkg/net/dial_test.go
index 5f5aea146a..3881953bb5 100644
--- a/src/pkg/net/dial_test.go
+++ b/src/pkg/net/dial_test.go
@@ -32,7 +32,7 @@ func TestDialTimeout(t *testing.T) {
 	numConns := listenerBacklog + 10
 
 	// TODO(bradfitz): It's hard to test this in a portable
-	// way. This is unforunate, but works for now.
+	// way. This is unfortunate, but works for now.
 	switch runtime.GOOS {
 	case "linux":
 		// The kernel will start accepting TCP connections before userspace
diff --git a/src/pkg/net/net_test.go b/src/pkg/net/net_test.go
index d4409cc491..fd145e1d70 100644
--- a/src/pkg/net/net_test.go
+++ b/src/pkg/net/net_test.go
@@ -11,14 +11,6 @@ import (
 	"time"
 )
 
-// avoidOSXFirewallDialogPopup avoids OS X, former konwn as MacOS X,
-// firewall dialog popups during tests.  It looks like OS X checks
-// wildcard listens by default for security reasons.  A listen with
-// specific address doesn't make dialog popups for now.
-var avoidOSXFirewallDialogPopup = func() bool {
-	return testing.Short() && runtime.GOOS == "darwin"
-}
-
 func TestShutdown(t *testing.T) {
 	if runtime.GOOS == "plan9" {
 		t.Logf("skipping test on %q", runtime.GOOS)
diff --git a/src/pkg/net/server_test.go b/src/pkg/net/server_test.go
index d5ea789880..158b9477d0 100644
--- a/src/pkg/net/server_test.go
+++ b/src/pkg/net/server_test.go
@@ -28,7 +28,7 @@ func skipServerTest(net, unixsotype, addr string, ipv6, ipv4map, linuxonly bool)
 	}\n \tswitch addr {\n \tcase \"\", \"0.0.0.0\", \"[::ffff:0.0.0.0]\", \"[::]\":\n-\t\tif avoidOSXFirewallDialogPopup() {\n+\t\tif testing.Short() || !*testExternal {\n \t\t\treturn true\n \t\t}\n \t}\ndiff --git a/src/pkg/net/unicast_test.go b/src/pkg/net/unicast_test.go
index fc6089414d..8bfe2796e0 100644
--- a/src/pkg/net/unicast_test.go
+++ b/src/pkg/net/unicast_test.go
@@ -50,7 +50,7 @@ func TestTCPListener(t *testing.T) {
 	}\n \n \tfor _, tt := range listenerTests {\n-\t\tif tt.wildcard && avoidOSXFirewallDialogPopup() {\n+\t\tif tt.wildcard && (testing.Short() || !*testExternal) {\n \t\t\tcontinue\n \t\t}\n \t\tif tt.ipv6 && !supportsIPv6 {\n@@ -98,7 +98,7 @@ func TestUDPListener(t *testing.T) {
 	}\n \n \tfor _, tt := range listenerTests {\n-\t\tif tt.wildcard && avoidOSXFirewallDialogPopup() {\n+\t\tif tt.wildcard && (testing.Short() || !*testExternal) {\n \t\t\tcontinue\n \t\t}\n \t\tif tt.ipv6 && !supportsIPv6 {\n@@ -132,7 +132,7 @@ func TestSimpleTCPListener(t *testing.T) {
 	}\n \n \tfor _, tt := range listenerTests {\n-\t\tif tt.wildcard && avoidOSXFirewallDialogPopup() {\n+\t\tif tt.wildcard && (testing.Short() || !*testExternal) {\n \t\t\tcontinue\n \t\t}\n \t\tif tt.ipv6 {\n@@ -170,7 +170,7 @@ func TestSimpleUDPListener(t *testing.T) {
 	}\n \n \tfor _, tt := range listenerTests {\n-\t\tif tt.wildcard && avoidOSXFirewallDialogPopup() {\n+\t\tif tt.wildcard && (testing.Short() || !*testExternal) {\n \t\t\tcontinue\n \t\t}\n \t\tif tt.ipv6 {\n@@ -267,7 +267,7 @@ func TestDualStackTCPListener(t *testing.T) {
 	}\n \n \tfor _, tt := range dualStackListenerTests {\n-\t\tif tt.wildcard && avoidOSXFirewallDialogPopup() {\n+\t\tif tt.wildcard && (testing.Short() || !*testExternal) {\n \t\t\tcontinue\n \t\t}\n \t\tswitch runtime.GOOS {\n@@ -316,7 +316,7 @@ func TestDualStackUDPListener(t *testing.T) {
 	}\n \n \tfor _, tt := range dualStackListenerTests {\n-\t\tif tt.wildcard && avoidOSXFirewallDialogPopup() {\n+\t\tif tt.wildcard && (testing.Short() || !*testExternal) {\n \t\t\tcontinue\n \t\t}\n \t\ttt.net1 = toudpnet(tt.net1)\n@@ -531,7 +531,7 @@ func TestProhibitionaryDialArgs(t *testing.T) {
 \t\treturn\n \t}\n \t// This test requires both IPv6 and IPv6 IPv4-mapping functionality.\n-\tif !supportsIPv4map || avoidOSXFirewallDialogPopup() {\n+\tif !supportsIPv4map || testing.Short() || !*testExternal {\n \t\treturn\n \t}\n \n```

## コアとなるコードの解説

### `src/pkg/net/dial_test.go`

```go
 // TODO(bradfitz): It's hard to test this in a portable
-// way. This is unforunate, but works for now.
+// way. This is unfortunate, but works for now.

この変更は、コメント内のスペルミス unforunateunfortunate に修正したものです。機能的な変更はありません。

src/pkg/net/net_test.go

-// avoidOSXFirewallDialogPopup avoids OS X, former konwn as MacOS X,
-// firewall dialog popups during tests.  It looks like OS X checks
-// wildcard listens by default for security reasons.  A listen with
-// specific address doesn't make dialog popups for now.
-var avoidOSXFirewallDialogPopup = func() bool {
-	return testing.Short() && runtime.GOOS == "darwin"
-}

avoidOSXFirewallDialogPopup という変数の定義が削除されました。この変数は、macOS環境で go test -short が実行された場合に true を返すように設計されており、ファイアウォールダイアログのポップアップを回避するためのOS固有のロジックをカプセル化していました。この削除により、より汎用的なスキップロジックに置き換えられることになります。

src/pkg/net/server_test.go および src/pkg/net/unicast_test.go

これらのファイルでは、avoidOSXFirewallDialogPopup() の呼び出し箇所が、testing.Short() || !*testExternal という新しい条件に置き換えられています。

例:

-\t\tif avoidOSXFirewallDialogPopup() {
+\t\tif testing.Short() || !*testExternal {
 \t\t\treturn true
 \t\t}
  • tt.wildcard: テストがワイルドカードアドレス(例: 0.0.0.0[::])でリッスンしようとしているかどうかを示すブール値。
  • testing.Short(): go test -short が実行された場合に true を返します。
  • !*testExternal: testExternal フラグが設定されていない場合に true を返します。

この変更により、ワイルドカードアドレスでリッスンするテストは、以下のいずれかの条件が満たされた場合にスキップされます。

  1. go test -short が実行された場合。
  2. testExternal フラグが明示的に指定されていない場合。

これにより、macOSのファイアウォールダイアログの問題が解決されるだけでなく、all.bash テストがデフォルトで外部ネットワークを使用しないという、より一般的なポリシーが適用されます。これは、テストの実行環境に依存しない、より安定したテストスイートの実現に貢献します。

関連リンク

参考にした情報源リンク