title: How to unit test OpenGL on EC2
body: <p>Both my local computer and EC2 server is on Ubuntu 14.04. Suppose I am testing a cuda opengl interop code as below. </p>
  
  <p>Test.cu</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #include &lt;GL/glew.h&gt;
  #include &lt;GLFW/glfw3.h&gt;
  #include &lt;cuda_gl_interop.h&gt;
  
  __global__ static void CUDAKernelTEST(float *data){
    const int x  = blockIdx.x * blockDim.x + threadIdx.x;
    const int y  = blockIdx.y * blockDim.y + threadIdx.y;
    const int mx = gridDim.x * blockDim.x;
  
    data[y * mx + x] = 0.5;
  }
  
  GLFWwindow *glfw_window_;
  
  void Setup(){
    if (!glfwInit()) exit(EXIT_FAILURE);
  
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
  
    glfw_window_ = glfwCreateWindow(10, 10, "", NULL, NULL);
  
    if (!glfw_window_) glfwTerminate();
  
    glfwMakeContextCurrent(glfw_window_);
  
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) exit(EXIT_FAILURE);
  }
  
  void TearDown(){
    glfwDestroyWindow(glfw_window_);
    glfwTerminate();
  }
  
  int main(){
    Setup();
  
    GLuint id;
    glGenBuffers(1, &amp;id);
    glBindBuffer(GL_ARRAY_BUFFER, id);
    glBufferData(GL_ARRAY_BUFFER, 3 * 24 * sizeof(GLfloat), 0, GL_STATIC_DRAW);
    cudaGraphicsResource *vbo_res;
    cudaGraphicsGLRegisterBuffer(&amp;vbo_res, id, cudaGraphicsMapFlagsWriteDiscard);
    cudaGraphicsMapResources(1, &amp;vbo_res, 0);
    float *test;
    size_t size;
    cudaGraphicsResourceGetMappedPointer(
    reinterpret_cast&lt;void **&gt;(&amp;test), &amp;size, vbo_res);
    dim3 blks(1, 1);
    dim3 threads(72, 1);
    CUDAKernelTEST&lt;&lt;&lt;blks, threads&gt;&gt;&gt;(test);
    cudaDeviceSynchronize();
    cudaGraphicsUnmapResources(1, &amp;vbo_res, 0);
  
    // do some more with OpenGL
  
    std::cout &lt;&lt; "you passed the test" &lt;&lt; std::endl;
  
    TearDown();
  
    return 0;
  }
  </code></pre>
  
  <p>The current approach is create a hidden window and a context. The code compiles and runs fine on my local machine. However, glfwInit() returns GL_FALSE when run on EC2. If I log the messages sent to the error callback, it shows "X11: The DISPLAY environment variable is missing", which looks like it needs a display monitor to be connected in order for it work. </p>
  
  <p>I tried replacing the Setup and TearDown section from GLFW into SDL or GLX and it returns similar error seemingly also requiring a display monitor attached. </p>
  
  <p>I also try running the code with Xvfb and Xdummy which is supposedly to faked a monitor but I got error message from Xvfb "Xlib:  extension "GLX" missing on display ":99", and from Xdummy "Fatal server error: (EE) no screens found(EE)"</p>
  
  <p>I can't be the first one attempting to unit test opengl related code on EC2, but I can't find any solutions after googling around. Please advice, thank you so much.</p>