bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.7k stars 3.98k forks source link

Document how to run (GNU/Linux) GUI tests in Bazel #22446

Open radoye opened 1 month ago

radoye commented 1 month ago

Description of the bug:

It is unclear how to run GUI tests. For example

bazel test //:my_gui_test --test_arg=--enable_display=true --test_env=DISPLAY --test_output=all

where :my_gui_test contains

  if (glfwInit() != GLFW_TRUE) {
    return absl::AbortedError("GLFW failed to initialize.");
  }

fails with logs (logging code not displayed)

Authorization required, but no authorization protocol specified
[16:19:58.798375] [error] GLFW error 65544: X11: Failed to open display :0

The same code works just fine in a cc_binary when actually executed in the user environment. It seems that, for some reason, test code cannot connect to X11 display.

So, something is different about the test environment in this respect, but I can't seem to find any mention in the docs. Any pointers? Thanks!

Which category does this issue belong to?

No response

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Try to open a window from a test, e.g. using GLFW like in the description.

Which operating system are you running Bazel on?

Ubuntu 24.04, Debian testing

What is the output of bazel info release?

release 7.1.2

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

Tangentially related: https://bazel.build/docs/android-instrumentation-test

Any other information, logs, or outputs that you want to share?

No response

oquenchil commented 3 weeks ago

Could you please provide a small repro?

radoye commented 3 weeks ago

Sure! In the interest of a timely response I'm creating one by butchering my actual situation, though it might not be the smallest possible repro. Sorry about that.

💣bazel --version

bazel 7.2.0
💣cat .bazelrc 

build:cpp20 --cxxopt=-std=c++20
💣cat MODULE.bazel

module(
    name = "bazel_ui_debug",
)

http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")                               
http_archive(
    name = "glfw",
    build_file = "third_party/glfw/BUILD",
    sha256 = "55261410f8c3a9cc47ce8303468a90f40a653cd8f25fb968b12440624fb26d08",
    strip_prefix = "glfw-3.3.9",
    urls = [
        "https://github.com/glfw/glfw/releases/download/3.3.9/glfw-3.3.9.zip",
    ],
)

bazel_dep(name = "abseil-cpp", version = "20240116.2")
bazel_dep(name = "googletest", version = "1.14.0")
💣cat BUILD

cc_library(
    name = "glfw_window",
    srcs = ["glfw_window.cpp"],
    hdrs = ["glfw_window.h"],
    deps = [
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:statusor",
        "@glfw",
    ],
)

# bazel run --config=cpp20 :window_print
cc_binary(
    name = "window_print",
    srcs = ["window_print.cpp"],
    deps = [
        ":glfw_window",
    ],
)

cc_test(
    name = "window_test",
    srcs = ["window_test.cpp"],
    deps = [
        ":glfw_window",
        "@abseil-cpp//absl/status",
        "@googletest//:gtest_main",
    ],
)
💣cat glfw_window.h

#pragma once

#include <cstddef>
#include <memory>

#include "GLFW/glfw3.h"
#include "absl/status/statusor.h"

namespace example {

struct Rect {
  int h;
  int w;
};

absl::StatusOr<GLFWwindow*> CreateWindow(size_t height, size_t width);

Rect GetWindowSize(GLFWwindow* window);

} // namespace example
💣cat glfw_window.cpp

#include "glfw_window.h"

#include "absl/status/status.h"
#include <iostream>

namespace example {
namespace {
void error_callback(int error, const char* description)
{
    fprintf(stderr, "Error: %s\n", description);
}
}  // namespace

absl::StatusOr<GLFWwindow*> CreateWindow(size_t height, size_t width) {
  glfwSetErrorCallback(error_callback);
  if (!glfwInit()) {
    return absl::InternalError("GLFW failed to init.");
  }

  GLFWwindow *window =
    glfwCreateWindow(width, height, "My Window Title", nullptr, nullptr);
  if (window == nullptr) {
    return absl::InternalError("GLFW cannot open a window.");
  }
  return window;
}

Rect GetWindowSize(GLFWwindow* window) {
  int h = -1;
  int w = -1;
  glfwGetWindowSize(window, &w, &h);

  return {h, w};
}

} // namespace example
💣cat window_print.cpp

#include "glfw_window.h"
#include <algorithm>
#include <iostream>

int main(int argc, char* argv[]) {

  auto result = example::CreateWindow(800, 600);
  if (!result.ok()) return -1;

  auto win = result.value();

  auto size = example::GetWindowSize(win);
  std::cout << "Created a window with size " << size.h << " x " << size.w;

  return 0;
}
💣cat window_test.cpp

#include "glfw_window.h"

#include "gtest/gtest.h"

namespace example {
namespace {

TEST(CreateWindowTest, CreateIsOk) { EXPECT_TRUE(CreateWindow(800, 600).ok()); }
} // namespace
} // namespace example

With this setup I get the following

💣bazel run :window_print --config=cpp20
WARNING: Build option --test_env has changed, discarding analysis cache (this can be expensive, see https://bazel.build/advanced/performance/iteration-speed).
INFO: Analyzed target //:window_print (0 packages loaded, 820 targets configured).
INFO: Found 1 target...
Target //:window_print up-to-date:
  bazel-bin/window_print
INFO: Elapsed time: 0.722s, Critical Path: 0.16s
INFO: 2 processes: 1 internal, 1 processwrapper-sandbox.
INFO: Build completed successfully, 2 total actions
INFO: Running command line: bazel-bin/window_print
Created a window with size 800 x 600

💣bazel test :window_test --config=cpp20 --test_output=all --test_arg=--enable_display=true --test_env=DISPLAY
WARNING: Build option --test_env has changed, discarding analysis cache (this can be expensive, see https://bazel.build/advanced/performance/iteration-speed).
INFO: Analyzed target //:window_test (0 packages loaded, 905 targets configured).
FAIL: //:window_test (see /home/rrs/.cache/bazel/_bazel_rrs/c00b1d92cdf325e8d3c94cb1321cbae9/execroot/_main/bazel-out/k8-fastbuild/testlogs/window_test/test.log)
INFO: From Testing //:window_test:
==================== Test output for //:window_test:
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from CreateWindowTest
[ RUN      ] CreateWindowTest.CreateIsOk
Authorization required, but no authorization protocol specified

Error: X11: Failed to open display :0
window_test.cpp:8: Failure
Value of: CreateWindow(800, 600).ok()
  Actual: false
Expected: true

[  FAILED  ] CreateWindowTest.CreateIsOk (0 ms)
[----------] 1 test from CreateWindowTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] CreateWindowTest.CreateIsOk

 1 FAILED TEST
================================================================================
INFO: Found 1 test target...
Target //:window_test up-to-date:
  bazel-bin/window_test
INFO: Elapsed time: 0.596s, Critical Path: 0.10s
INFO: 2 processes: 1 internal, 1 processwrapper-sandbox.
INFO: Build completed, 1 test FAILED, 2 total actions
//:window_test                                                           FAILED in 0.1s
  /home/rrs/.cache/bazel/_bazel_rrs/c00b1d92cdf325e8d3c94cb1321cbae9/execroot/_main/bazel-out/k8-fastbuild/testlogs/window_test/test.log

Executed 1 out of 1 test: 1 fails locally.

I hope this helps.

radoye commented 3 weeks ago

Oh, and of course

💣uname -a

Linux forge 6.8.0-35-generic #35-Ubuntu SMP PREEMPT_DYNAMIC Mon May 20 15:51:52 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

I believe the only system provided library needed is libx11-dev that is referenced when linking ...

💣cat third_party/glfw/BUILD

package(default_visibility = ["//visibility:public"])                                                                      

## GLFW targets                                                                                                            
# References                                                                                                               
#   https://github.com/pbellis/bazel-glfw                                                                                  

GLFW_LINUX_DEFINES = [                                                                                                     
    "_GLFW_HAS_XF86VM",                                                                                                    
    "_GLFW_X11",                                                                                                           
]                                                                                                                          

GLFW_LINUX_HDRS = [                                                                                                        
    "src/glx_context.h",                                                                                                   
    "src/linux_joystick.h",                                                                                                
    "src/posix_thread.h",                                                                                                  
    "src/posix_time.h",                                                                                                    
    "src/x11_platform.h",                                                                                                  
]                                                                                                                          

GLFW_LINUX_SRCS = [                                                                                                        
    "src/glx_context.c",
    "src/linux_joystick.c",
    "src/posix_thread.c",
    "src/posix_time.c",
    "src/x11_init.c",
    "src/x11_monitor.c",
    "src/x11_window.c",
]

GLFW_LINUX_LINKOPTS = [
    "-lX11",
]

GLFW_COMMON_HDRS = [
    "include/GLFW/glfw3.h",
    "include/GLFW/glfw3native.h",
    "src/egl_context.h",
    "src/internal.h",
    "src/osmesa_context.h",
    "src/mappings.h",
    "src/xkb_unicode.h",
]

GLFW_COMMON_SRCS = [
    "src/context.c",
    "src/egl_context.c",
    "src/init.c",
    "src/input.c",
    "src/osmesa_context.c",
    "src/monitor.c",
    "src/vulkan.c",
    "src/window.c",
    "src/xkb_unicode.c",
]

cc_library(
    name = "glfw_src",
    srcs = GLFW_COMMON_SRCS + select({
        "@bazel_tools//src/conditions:linux_x86_64": GLFW_LINUX_SRCS,
    }),
    hdrs = GLFW_COMMON_HDRS + select({
        "@bazel_tools//src/conditions:linux_x86_64": GLFW_LINUX_HDRS,
    }),
    defines = select({
        "@bazel_tools//src/conditions:linux_x86_64": GLFW_LINUX_DEFINES,
    }),
    deps = [],
)

cc_library(
    name = "glfw",
    hdrs = [
        "include/GLFW/glfw3.h",
        "include/GLFW/glfw3native.h",
    ],
    copts = [
        "-Iexternal/glfw/include/",
    ],
    linkopts = select({
        "@bazel_tools//src/conditions:linux_x86_64": GLFW_LINUX_LINKOPTS,
    }),
    strip_include_prefix = "include",
    deps =
        select({
            "@bazel_tools//src/conditions:linux_x86_64": [":glfw_src"],
        }),
)

Did I mention this might not be the smallest example?